Jump to content

WHILE loop within a WHILE loop


ridley1012

Recommended Posts

Hi everyone. Does anyone know if it is possible to have a while loop within a while loop. I am currently trying to create a function to display a number of select boxes dependent on a number posted from a previous page. I would then like these select boxes to all have the same data pulled from a database. The best I can do at the moment is display the select boxes but only the first select box fills with data. Thanks to anyone who can help in advance  ;D

<form action="addBlaydonResultProcess.php" method="post" enctype="multipart/form-data" name="addGS">
<input name="GS" type="hidden" value="" />
  		<table width="400" border="0">
    		<?php
			$players="SELECT * FROM player ORDER BY firstname";
			$playerResult =mysql_query($players); 
			$temp = $homeTeamScore;
			$i=0;
	while($i!=$temp)
		{
			$count = $i + 1;
		?>
        <tr>
            <td width =\"150\">Goal Scorer <?php print $count ?> :
            </td>
            <td width=\"330\"><select name="scorer[]">
          		<?php 
				while ($row = mysql_fetch_array($playerResult)) 
				{

      				$playerID = $row[0];
				$firstname = $row[2];
				$surname = $row[3];

			?>
                <option value="<?php print $playerID; ?>"><?php print ucwords($firstname);?> <?php print ucwords($surname);?></option>
	 <?php } mysql_free_result($playerResult); ?>
						  </select>
            </td>
    	</tr>
    <?php 
		$i++; }
?>
    	<tr>
          <td width ="50"></td>
          <td width="300"><input name="btnUpload" type="submit" id="btnUpload" value="Add Goalscorers" /></td>
    	</tr>
  		</table>
</form>

Link to comment
https://forums.phpfreaks.com/topic/143705-while-loop-within-a-while-loop/
Share on other sites

Yes, you can absolutely have while loops within while loops. The problem is that you use mysql_free_result() after the first loop. So, there is no more data when the loop runs the second time. You would want to reset the pointer to the first row in the result set.

 

But, there is a MUCH easier solution to your problem. Just create the select list once as a variable, then redisplay it as many time as you want. I would also keep all the PHP and HTML separate. Makes it much easier to work with in my opinion

<?php

$players="SELECT * FROM player ORDER BY firstname";
$playerResult =mysql_query($players);
$temp = $homeTeamScore;
$i=0;

//Create the common select list
$select_list = "<select name=\"scorer[]\">\n";
while ($row = mysql_fetch_array($playerResult)) 
{
    $select_list .= "<option value=\"{$row[0]}\">".ucwords($row[2])." ".ucwords($row[3])."</option>\n";
}
$select_list .= "</select>\n";
mysql_free_result($playerResult);

//Create the table rows
$rows = "";
for($count=1; $count<=$temp; $count++)
{
    $rows .= "<tr>\n";
    $rows .= "    <td width =\"150\">Goal Scorer $count :</td>\n";
    $rows .= "    <td width=\"330\">$select_list</td>\n";
    $rows .= "</tr>\n";
}
?>

<form action="addBlaydonResultProcess.php" method="post" enctype="multipart/form-data" name="addGS">
<input name="GS" type="hidden" value="" />
    <table width="400" border="0">
        <?php echo $rows; ?>
        <tr>
            <td width ="50"></td>
            <td width="300"><input name="btnUpload" type="submit" id="btnUpload" value="Add Goalscorers" /></td>
        </tr>
    </table>
</form>

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.