golfer Posted October 18, 2010 Share Posted October 18, 2010 I currently have a problem where I'm trying to list alot of products with the category heading only being returned once and then the long list of products within that category being listed. For Example Football boots Nike Addidas Puma etc Whereas at the moment I'm getting. Football boots Nike Football boots Addidas Football boots Puma My current code for this is as follows. <?php while($row = $db->fetchrow($stuff)) { ?> <li><a href="store-<?php echo $row['cat_id']; ?>/<?php echo seo_makeSafeURI($row['cat_title']); ?>.html"><strong><?php echo stripslashes(htmlentities($row['cat_title'])); ?></strong></a></li> <li><a href="store-<?php echo $row['cat_id']; ?>-<?php echo $row['prod_id']; ?>/<?php echo seo_makeSafeURI($row['prod_title']); ?>.html"><?php echo stripslashes(htmlentities($row['prod_title'])); ?></a></li> <?php } ?> Any help would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/216151-how-to-print-single-variable-in-while-loop/ Share on other sites More sharing options...
litebearer Posted October 18, 2010 Share Posted October 18, 2010 Might look into GROUP BY Quote Link to comment https://forums.phpfreaks.com/topic/216151-how-to-print-single-variable-in-while-loop/#findComment-1123377 Share on other sites More sharing options...
golfer Posted October 18, 2010 Author Share Posted October 18, 2010 I tried Group BY cat_title but this just brought back the category and only one product that was in that category so tried GROUP_BY cat_title, prod_title just brought back the same result as before. Quote Link to comment https://forums.phpfreaks.com/topic/216151-how-to-print-single-variable-in-while-loop/#findComment-1123382 Share on other sites More sharing options...
PFMaBiSmAd Posted October 18, 2010 Share Posted October 18, 2010 Think about this, how would a human perform this task? You would 'remember' the current category and only process a category heading when the category name changes. How do computers 'remember' values? They store them in variables. How do computers test if a value changes? They use an if(){} conditional test. $current_category = ''; // initialize to a value that will never exist as data while($row = mysql_fetch_assoc($result)){ // test if the category changed if($current_category != $row['cat_title']){ // category changed (or it is the first one detected), output the heading here .... // remember the current category for the next test $current_category = $row['cat_title']; } // output the product data here .... } Quote Link to comment https://forums.phpfreaks.com/topic/216151-how-to-print-single-variable-in-while-loop/#findComment-1123387 Share on other sites More sharing options...
golfer Posted October 18, 2010 Author Share Posted October 18, 2010 That makes sense thankyou. I am however getting an error from my updated code. For the variable $current_category can you call this anything? <?php $current_category='cat'; while($row = $db->query($stuff)) { if($current_category != $row['cat_title']){ ?> <li><a href="store-<?php echo $row['cat_id']; ?>/<?php echo seo_makeSafeURI($row['cat_title']); ?>.html"><strong><?php echo stripslashes(htmlentities($row['cat_title'])); ?></strong></a></li> <?php $current_category=$row['cat_title']; ?> <li><a href="store-<?php echo $row['cat_id']; ?>-<?php echo $row['prod_id']; ?>/<?php echo seo_makeSafeURI($row['prod_title']); ?>.html"><?php echo stripslashes(htmlentities($row['prod_title'])); ?></a></li> <?php } }?> Quote Link to comment https://forums.phpfreaks.com/topic/216151-how-to-print-single-variable-in-while-loop/#findComment-1123399 Share on other sites More sharing options...
BlueSkyIS Posted October 18, 2010 Share Posted October 18, 2010 what is the error? Quote Link to comment https://forums.phpfreaks.com/topic/216151-how-to-print-single-variable-in-while-loop/#findComment-1123408 Share on other sites More sharing options...
golfer Posted October 18, 2010 Author Share Posted October 18, 2010 On my website it just says database query error. But have run the query straight into the database and it works fine. So am unsure of what exactly is wrong with the code. Quote Link to comment https://forums.phpfreaks.com/topic/216151-how-to-print-single-variable-in-while-loop/#findComment-1123415 Share on other sites More sharing options...
BlueSkyIS Posted October 18, 2010 Share Posted October 18, 2010 echo the SQL and see what it looks like. Quote Link to comment https://forums.phpfreaks.com/topic/216151-how-to-print-single-variable-in-while-loop/#findComment-1123420 Share on other sites More sharing options...
golfer Posted October 18, 2010 Author Share Posted October 18, 2010 The Sql is fine and does exactly what I need. The error is in the code I have changed to make the title only appear once. I will continue to change things to see what works. Select * from (products LEFT JOIN categories_products_link ON products.prod_id = categories_products_link.prod_id) LEFT JOIN categories ON categories_products_link.cat_id = categories.cat_id; Quote Link to comment https://forums.phpfreaks.com/topic/216151-how-to-print-single-variable-in-while-loop/#findComment-1123424 Share on other sites More sharing options...
anselk Posted October 27, 2010 Share Posted October 27, 2010 perhaps array_unique($foo); where $foo is the array that you are outputting from your DB. $new_row = array_unique($row); In your case Quote Link to comment https://forums.phpfreaks.com/topic/216151-how-to-print-single-variable-in-while-loop/#findComment-1127117 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.