How to query and output data in a nested loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yess
    New Member
    • Feb 2011
    • 7

    How to query and output data in a nested loop?

    I'm trying to implement a function which will print all all the diferent type of products and their makes. I have 3 types of products and 6 makes. The problem is, it only prints one type of product 1 with the different makes, and then doesnt loop again to print the other types.


    Here is my code:

    Code:
    function search_product($types, $makes, $orgProd, $orgName){
    		 
    		 
    require_once ('../mysqli_connect.php');
    		
    	$makesId=count($makes); 
    	$typesId=count($types); 
    
    echo '<form  action="proccessIt.php"  method="post" > ';
    				
    echo'<fieldset>';
    
    
    
    
     for($i=0; $i<$typesId; $i++){
     
    	 echo ' type_i: '. $type=$types[$i];	
    		
    		
    		
    								for($i=0; $i<$makesId; $i++){
    							         $make=$makes[$i];	
    
    								
            //print product type as header 
    		
    		
            // Retrieve product model and  id:
    		
    		echo $q = "SELECT model AS  product_model, id_product AS id_product  FROM product WHERE id_brand=$make AND  id_type=$type;";
    	
    		
    		$r =mysqli_query ($dbc, $q) or die(mysqli_error($dbc)); // Run the query.
    		
    
    		
    		if ($r) { // If it ran OK, display the records.
    
    				
    				
    				echo'	<table align="center" cellspacing="3" cellpadding="3" width="75%">
    				<tr><td align="left"><b><div id="prodName"> '.$typeLoop.' </div><br/></b></td></tr>
    				';
    	
    				// Fetch and print all the records:
    				while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    	
    				
    				
    				echo '<tr>  <td align="left">'  .  
    				' <input type="checkbox" name="checkbox[]checkbox2[]" value="'. $row['id_product'] .'" value="'. $row['product_model']  .'">
    				'. $row['product_model'] .'  
                       </b>    </td>   </tr>
    				';
    				
    				}//end of while
    				
    			
    		mysqli_free_result ($r); // Free up the resources.	
    
    		} else { // If it did not run OK.
    
    		// Public message:
    		echo '<p class="error">The current request could not be retrieved. We apologize for any inconvenience.</p>';
    	
    		// Debugging message:
    		echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
    	
    		
    		
    		} // End of else.
    
    
    
    
    		}//end of second for loop
    	}//end of for loop
    	
    	echo '<tr>  <td align="left">'  . 
    				'<input type="hidden" name="orgProd"  value="'.$orgProd.'" />
    				  </td>   </tr>
    				';
    				
    				
    				echo '<tr>  <td align="left">'  . 
    				'<input type="hidden" name="orgName"  value="'.$orgName.'" />
    				  </td>   </tr>
    				';
    	
    	
    echo '
    			  </table>
    			  <input type="submit" value="Search">
    			  <input type="hidden" name="submitte" value="7"/>
    			  
    			  </fieldset>
    			  </form>'; // Close the table.
    
    mysqli_close($dbc); // Close the database connection.
    		
    		
    		
    }//end of product function
    Last edited by Niheel; Feb 5 '11, 08:17 PM. Reason: code tags
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #2
    Use foreach instead.

    Code:
    foreach($types as $type) {
      foreach($makes as $make) {
        // You've now got $make and $type as variables
        }
      }
    It'll loop through each item in the array. So for each type you'll loop through the entire make array.

    Comment

    • Yess
      New Member
      • Feb 2011
      • 7

      #3
      Hi Halo2FrEeEk,

      I only just seen your reply. Already test it and it works!

      Thanks you very much, it had already taken me to long to get it to work... I cant beleive it was so simple!

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        The foreach is by far the best way to get this working. But for future reference the us of $i in both the inner and outer loop is bad parctice. I recommend using $i for the outer and $j for the inner if you want to use short variable names. Alternatively call them proper names - if the loops get deep it'll make maintenance easier.

        Cheers
        nathj

        Comment

        Working...