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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.