anthonydamasco Posted October 21, 2008 Share Posted October 21, 2008 Hola I have a while loop issue that is driving me crazy - I have a database with the columns | Category | Subcat 1 | Subcat 2 | Subcat 3 | Subcat 4 | Productname under "sub cat 4" I have this data: Red Star Red Star Red Star Red Star Red Star BioSpringer BioSpringer BioSpringer BioSpringer AEB Yeast AEB Yeast AEB Yeast AEB Yeast I need to display products that are under "subcat 4" only mentioning "subcat 4" once then displaying all products below it, when the subcat 4 changes to the next result i needs to change. Example below. Red Star product product product product BioSpringer product product product product AEB Yeast product product product product product My code $sql3 = "select product_id, product_name, subcategory_4 from products where category = '$category' and subcategory_1 = '$subcategory_1' and subcategory_2 = '$subcategory_2' and subcategory_3 = '$subcategory_3'"; $query3 = mysql_query($sql3) or print "<font>insert error: " . mysql_error() . ". SQL was $sql"; $num_rows_top3 = mysql_num_rows($query3); if ($num_rows_top3 !== 0 ) { $sql4 = "select distinct subcategory_4 from products where category = '$category' and subcategory_1 = '$subcategory_1' and subcategory_2 = '$subcategory_2' and subcategory_3 = '$subcategory_3'"; $query4 = mysql_query($sql4) or print "<font>insert error: " . mysql_error() . ". SQL was $sql"; $m = 0; $q = 0; while (list($subcategory_4_loop)=mysql_fetch_row($query4)) { $m++; if ($m == 1) { echo "<br /><div class=\"product_nav_right_header padding_bottom_15\">$subcategory_4_loop</div>"; } #List all the products within this statement. while (list($product_id, $product_name, $subcategory_4)=mysql_fetch_row($query3)) { if ($subcategory_4 !== $subcategory_4_loop) { $q++; if ($q == 1) { echo "<br /><div class=\"product_nav_right_header padding_bottom_15\">$subcategory_4</div>"; } } echo "<a href=\"product_detail2.php?product_id=$product_id&id=$k\" class=\"right_side_link\">$product_name</a><br>"; } #end while loop What my code is doing is: Red Star product product product product BioSpringer product product product product product product It's not listing any subcategory after the first 2 - I know it's because of my counter - but I can't think of any other way to do it. Any help would be great Quote Link to comment Share on other sites More sharing options...
Barand Posted October 21, 2008 Share Posted October 21, 2008 here's the basic method <?php $res = mysql_query ("SELECT subcat4, productname FROM products ORDER BY subcat4, productname"); $prevCat = ''; while (list($sc4, $prod) = mysql_fetch_row($res)) { if ($sc4 != $prevCat) { echo "<br/>$sc4<br/>"; $prevCat = $sc4; } echo "$prod<br/>"; } ?> Quote Link to comment Share on other sites More sharing options...
anthonydamasco Posted October 21, 2008 Author Share Posted October 21, 2008 what does $prevCat = why is it blank? I get the logic you are suggesting, I just don't know how to get fill in the if (subcat4 = prevcat) info wait, i get it, let me try it out Quote Link to comment Share on other sites More sharing options...
Barand Posted October 21, 2008 Share Posted October 21, 2008 It's initialised to blank to ensure it doesn't match the subcat4 in the first row thus forcing the subcat value to print. The logic is "When the value of the subcat changes, print the new value" Quote Link to comment Share on other sites More sharing options...
Barand Posted October 21, 2008 Share Posted October 21, 2008 Another way to do it <?php $res = mysql_query ("SELECT subcat4, GROUP_CONCAT(productname SEPARATOR '<br/>') FROM products GROUP BY subcat4"); while (list($sc4, $prods) = mysql_fetch_row($res)) { echo "<h3>$sc4</h3>"; echo $prods; } ?> Quote Link to comment Share on other sites More sharing options...
anthonydamasco Posted October 22, 2008 Author Share Posted October 22, 2008 thanks again Quote Link to comment 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.