homepage Welcome to WebmasterWorld Guest from 54.211.213.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Grouping results under a heading
Rather than have the heading repeat
abbeyvet




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

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

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

 

mattclayb




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

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)

idfer




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

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


Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved