Jump to content

Grouping A Query Output? .... Anyone Know How To Accomplish This?

Recommended Posts

so i have some data in my mysql database:name | type----------------playstation | ToyBlouse | Clothingapple | Foodnintendo | Toypants | Clothinghot dog | FoodI want to output it in groups so that on my page it looks something like this:ToyplaystationnintendoClothingBlousepantsFoodapplehot dogthis is my php code so far, but it just outputs each row in the table. But I only want one heading for each type.

$query = "SELECT type, name FROM products ORDER BY type DESC"; $result = mysql_query($query) or die(mysql_error());// Print out resultwhile($row = mysql_fetch_array($result)){ echo "<b>".$row['type']."</b><br/>"; echo $row['name']; echo "<br />";}
Anyone know how to accomplish this?
Link to post
Share on other sites

Have a variable that keeps track of the last heading you printed, and when the next record has a heading different from the value in that variable, print the heading and save the new value.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...