Forum Moderators: coopster
Set up>
table_items
-----------
item_id ¦ item_name ¦ item_describtion ¦ more item stuff.....
table_subitems
-----------------
subitem_id ¦ item_id ¦ subitem_name ¦ More subitem stuff
Each item can have zero to pretty much unlimited subitems, and are related by the fields item_id.
I want to print out the subitems grouped by item.
I am currently doing this with a query which selects subitems where table_items.item_id = table_subitems.item_id. Which is fine as far as it goes, I get something like this:
Item One - Subitem 0ne
Item One - Subitem two
Item One - Subitem three
Item Two - Subitem one
Item Three - Subitem one
Item Three - Subitem two
But I want
Item One
========
Subitem one
Subitem two
Subitem three
Item Two
========
Subitem one
Item Three
========
Subitem one
Subitem two
How do I do this? Is it a function of the query or of how I show the results?
Thanks
$qh = mysql_query('select item_name, subitem_name
from table_items, table_subitems
where table_items.item_id = table_subitems.item_id
order by item_name
');
if($qh === false)
die 'Mysql error: '.mysql_error(); $current_item_name = '';
while($row = mysql_fetch_array($qh)) {
if($row['item_name'] != $current_item_name) {
$current_item_name = $row['item_name'];
echo $current_item_name."\n=========\n\n";
}
echo $row['subitem_name']."\n";
}