Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Grouping results under a heading

Rather than have the heading repeat



5:00 pm on Jul 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

This is probably obvious and simple - but it isn't to me :)

Set up>

item_id ¦ item_name ¦ item_describtion ¦ more item stuff.....

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?



5:07 pm on Jul 20, 2009 (gmt 0)

10+ Year Member

you could use an inner join in your sql eg -

SELECT * FROM table_subitems subt INNER JOIN table_items itemst ON (subt.item_id=itemst.item_id)


6:29 pm on Jul 20, 2009 (gmt 0)

5+ Year Member

It's a function of how you show the results. You can do something like this:

$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";


Featured Threads

Hot Threads This Week

Hot Threads This Month