i have a mysql database. table : articles title : titles of articles category : categories of articles text : text of articles i want to print this to my webpage with a single php code: category 1 (category) 1. title 1 (title) .... text text text ........ (text) 2. title 2 (title) .... text text text ........ (text) category 2 (category) 1. title 1 (title) .... text text text ........ (text) 2. title 2 (title) .... text text text ........ (text) category 3 (category) 1. title 1 (title) .... text text text ........ (text) 2. title 2 (title) .... text text text ........ (text) my code is this : <? $resultb = mysql_query("SELECT category, title, text FROM articles GROUP BY category ORDER BY title"); $i=1; while ($resultat = mysql_fetch_array($resultb)) { $title=$resultat[title]; $text=$resultat[text]; $category=$resultat[category]; ?> <b><?=$category?></b><br> • <?=$title?> <br> <?=$text?> <? $i++; $s++; } ?> PHP: but i could not be successfull. please help me
I don't think that GROUP BY is what you are looking for then.. you are only going to get one row for each of the grouped results. How about something like this?: <? $lastcategory=''; $categorycount=0; $titlecount=0; $hDb = mysql_connect("localhost","mysqluser","mysqlpass"); mysql_select_db('test',$hDb); $resultb = mysql_query("SELECT category, title, text FROM articles ORDER BY category,title"); $i=1; while ($resultat = mysql_fetch_array($resultb)) { $titlecount++; $title=$resultat[title]; $text=$resultat[text]; $category=$resultat[category]; if ($category != $lastcategory) { $titlecount=1; $categorycount++; $lastcategory = $category; print "<br>category " . $categorycount . " (" . $category . ")<br>"; } print $titlecount . ". " . $title . "<br>" . $text . "<br>"; $i++; $s++; } ?> PHP: I think sorting by category then title and printing the category only when it has changed gets you closer to the results you are loooking for..
thank you very very very much. i have asked this question in several forums. but you are the only one who could help me. thanks a lot.