matto1376 Posted January 17, 2010 Share Posted January 17, 2010 Hi guys, I have a mySQL query that returns a product category and product like this: Cat Name 1 Product Name1 Cat Name 1 Product Name2 Cat Name 2 Product Name3 Cat Name 2 Product Name4 Cat Name 2 Product Name5 Using PHP, how can I out my query so that it only displays the Category name once?? ie: Cat Name 1 Product Name1 Product Name2 Cat Name 2 Product Name3 Product Name4 Product Name5 Thanks in advance for any suggestions. Link to comment https://forums.phpfreaks.com/topic/188742-listing-product-categories-and-their-product-names-have-mysql-need-some-php/ Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 Something like this will do the trick.. $old_cat = null; while ($data = mysql_fetch_assoc($sql_query)) { if (is_null($old_cat) || $old_cat != $data['category_name']) { echo $data['category_name']; $old_cat = $data['category_name']; } // echo out your products here } Link to comment https://forums.phpfreaks.com/topic/188742-listing-product-categories-and-their-product-names-have-mysql-need-some-php/#findComment-996345 Share on other sites More sharing options...
matto1376 Posted January 17, 2010 Author Share Posted January 17, 2010 Thanks Buddski, Real close....it got rid of the first category and products though..... So say for example I had initially: Cat Name 1 Product Name1 Cat Name 2 Product Name2 Cat Name 2 Product Name3 Cat Name 2 Product Name4 Cat Name 3 Product Name5 Cat Name 3 Product Name6 Your code would leave me with: Cat Name 2 Product Name2 Product Name3 Product Name4 Cat Name 3 Product Name5 Product Name6 So, real close, just dropping off that first category. Am I doing something wrong?? Thanks. Link to comment https://forums.phpfreaks.com/topic/188742-listing-product-categories-and-their-product-names-have-mysql-need-some-php/#findComment-996390 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 Um yes, you are... Actually I cant answer that question until I actually see what you are doing. Can you post your code. Just the part in question and the query etc. Link to comment https://forums.phpfreaks.com/topic/188742-listing-product-categories-and-their-product-names-have-mysql-need-some-php/#findComment-996392 Share on other sites More sharing options...
matto1376 Posted January 17, 2010 Author Share Posted January 17, 2010 Hi Buddski, All good....I adjusted my ORDER BY in mySQL, I had it ordering by a categoryID - changed it to ordering by itemID and it worked. Now....my next thing is to tabulate the data....I'm not too sure how to display your code in a table....I will try and figure that out now, unless there are any special tricks? Thanks heaps for your help so far!! Link to comment https://forums.phpfreaks.com/topic/188742-listing-product-categories-and-their-product-names-have-mysql-need-some-php/#findComment-996407 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 When you have the echo $data['category_name'] make that a <tr><td> with a colspan of the whole table.. so it will span all the columns and look like a header.. Then just display the others as normal.. $old_cat = null; while ($data = mysql_fetch_assoc($sql_query)) { if (is_null($old_cat) || $old_cat != $data['category_name']) { echo '<tr><td colspan="2">'.$data['category_name'].'</td></tr>'."\n"; $old_cat = $data['category_name']; } // echo out your products here echo '<tr> <td>'.$data['product_name'].'</td> <td>'.$data['price'].'</td> </tr>'."\n"; } Link to comment https://forums.phpfreaks.com/topic/188742-listing-product-categories-and-their-product-names-have-mysql-need-some-php/#findComment-996410 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.