Jump to content

[SOLVED] PHP while loop issue


anthonydamasco

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/129477-solved-php-while-loop-issue/
Share on other sites

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/>";
}
?>

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;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.