ridley1012 Posted February 3, 2009 Share Posted February 3, 2009 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 <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> Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 4, 2009 Share Posted February 4, 2009 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> Quote Link to comment Share on other sites More sharing options...
ridley1012 Posted February 4, 2009 Author Share Posted February 4, 2009 Thats brilliant mjdamato, great answer and worked perfectly. I've been trying for hours now nd can't believe it was as fairly simple as just creating a variable. Thanks again for your help. ;D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.