Can I use while in while?

Discussion in 'PHP' started by baris22, Oct 15, 2009.

  1. #1
    Hello,

    This is my query:

    
    
    		<? 
    		$query1 = "SELECT * FROM item WHERE order_reference_number='$ref'";
    	    $portfolio = mysql_query($query1);
    		while($row1 = mysql_fetch_array($portfolio)) { 
    		?>
                <br />
              <table width="100%">
                <tr>
                  <td>
    <input name="item_amount" type="text" value="<?=$row1['item_amount'];?>" size="2" /> of <input name="item_name" type="text" value="<?=$row1['item_name'];?>" size="30" /> </td>
      </tr>
      </table>
      <? } ?>      
    
    
    PHP:
    I want to add a select box

    
    
    <select name="select2" id="select2">
           <option value="Select">Select</option>
       <?	
        $query3 = "SELECT * FROM material";
    	$portfolio1 = mysql_query($query3);
    ?>
    <? while($row3 = mysql_fetch_array($portfolio1)) { ?>     
    
            <option>
    
    <?=$row3['material_name'];?>
     </option>
    <? } ?>
            </select>
    
    
    PHP:
     
    baris22, Oct 15, 2009 IP
  2. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Ok. never mind. This is working perfect. I was saving under another file name. that is why it was not working
     
    baris22, Oct 15, 2009 IP
  3. hostseller

    hostseller Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Just for references of other visitors and members...

    You can use while in a while... there is no such restriction. You should just be carefull about cpu/ram usage since a loop inside a loop may search data in millions of rows according to number of rows in tables.

    Regards,
     
    hostseller, Oct 15, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Correct, and since the while loop you have in the while is simply receiving the same data from the database for every row, you would do better to save this in an array outside of the while loops and then simply reference that array with foreach.
     
    premiumscripts, Oct 15, 2009 IP