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