Formatting output from database
am 16.03.2007 16:54:18 von Nosferatum
I wonder how to format the output from my db. I retrive the data fine,
but each post belong to a category (total 4 categories), and I would
like to have the output sorted under each category in a nice manner,
like this:
CATEGORY - NAME
post 1 data...
post 2 data ...
(space)
CATEGORY -NAME2
post 1 data...
post 2 data.. etc.
I just don't know how to do it. Help?
Re: Formatting output from database
am 16.03.2007 17:13:06 von Shion
Nosferatum wrote:
> I wonder how to format the output from my db. I retrive the data fine,
> but each post belong to a category (total 4 categories), and I would
> like to have the output sorted under each category in a nice manner,
> like this:
>
> CATEGORY - NAME
> post 1 data...
> post 2 data ...
>
> (space)
>
> CATEGORY -NAME2
> post 1 data...
> post 2 data.. etc.
>
> I just don't know how to do it. Help?
There are different options here, you could make 4 queries, one for each
category. Another way is to have one query and you store each category to an
array (you can also put those together in a multidimentional array) and then
loop throw each array when you want to generate the output.
--
//Aho
Re: Formatting output from database
am 16.03.2007 17:33:09 von luiheidsgoeroe
On Fri, 16 Mar 2007 17:13:06 +0100, J.O. Aho wrote:
> Nosferatum wrote:
>> I wonder how to format the output from my db. I retrive the data fine=
,
>> but each post belong to a category (total 4 categories), and I would
>> like to have the output sorted under each category in a nice manner,
>> like this:
>>
>> CATEGORY - NAME
>> post 1 data...
>> post 2 data ...
>>
>> (space)
>>
>> CATEGORY -NAME2
>> post 1 data...
>> post 2 data.. etc.
>>
>> I just don't know how to do it. Help?
>
> There are different options here, you could make 4 queries, one for ea=
ch
> category. Another way is to have one query and you store each category=
=
> to an
> array (you can also put those together in a multidimentional array) an=
d =
> then
> loop throw each array when you want to generate the output.
Also a possibility:
$query =3D 'SELECT category,post,data FROM table ORDER BY catagory';
$cat =3D '';
$result =3D mysql_query($query);
if($result){
while($row =3D mysql_fetch_assoc($result)){
if($row['category']!=3D$cat){
$cat =3D $row['category'];
echo "CATEGORY - {$cat}";
}
echo $row['post'],$row['data'];
}
}
-- =
Rik Wasmus