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
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>

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.