nertskull Posted June 19, 2010 Share Posted June 19, 2010 So I was playing with this earlier and can't figure it out. I have a loop displaying a small form. Within that loop I have another While loop to display a bunch of options. But if the first loop get run more then once, the 2nd loop doesn't run the next time. Here's the code. <?php $num_segments = $_POST['InputNumSelect']; for($i = 0; $i < $num_segments; $i++) {?> <p> <tr><td> <input name="Date[]"> </td><td><input name="Weight[]"> </td><td> <label> <select name="PersonSelect" id="PersonSelect"> <?php while ($row = mysql_fetch_array($result)) { echo "<option>" . $row['Name'] . ", " . $row['Nickname'] . "</option>"; } ?> </select> </label></td> </tr> </p> <?php } // EndFor formfields for number of segments needed?> So when that runs. I only get the first row showing. I attached a picture to explain. What am I doing wrong (obviously I'm pretty new to php) Thanks all! [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/205251-loops-within-loops/ Share on other sites More sharing options...
theverychap Posted June 19, 2010 Share Posted June 19, 2010 Your $result array has been iterated through to the end (looped through all elements, and so cannot loop through again). Try a reset just before your while loop: reset($result) while (... Quote Link to comment https://forums.phpfreaks.com/topic/205251-loops-within-loops/#findComment-1074352 Share on other sites More sharing options...
nertskull Posted June 19, 2010 Author Share Posted June 19, 2010 That unfortunately didn't work. I get the same result as above when I change the code to this <?php $num_segments = $_POST['InputNumSelect']; for($i = 0; $i < $num_segments; $i++) {?> <p> <tr><td> <input name="Date[]"> </td><td><input name="Weight[]"> </td><td> <label> <select name="PersonSelect" id="PersonSelect"> <?php reset($result); while ($row = mysql_fetch_array($result)) { echo "<option>" . $row['Name'] . ", " . $row['Nickname'] . "</option>"; } ?> </select> </label></td> </tr> </p> <?php } // EndFor formfields for number of segments needed?> I also tried putting the reset(result); part at various lines throughout the code (i.e. before the while, after the while, after echos, etc,) and nothing worked. I also tried various combinations of unset which also did nothing. I looked around and tried this $result=result(); which (from what I read) should reset the array, but it made it so I couldn't get two lines of the form. I must still be doing something wrong. Quote Link to comment https://forums.phpfreaks.com/topic/205251-loops-within-loops/#findComment-1074380 Share on other sites More sharing options...
PFMaBiSmAd Posted June 19, 2010 Share Posted June 19, 2010 You would need to use - mysql_data_seek Edit: Actually, since you are just processing the same data the same way each pass through the outer loop, it would be better if you formed the content in a variable and then just echo the contents of that variable anywhere you need it. No need to loop through the result set each time. If you were processing the data differently each time you accessed it, then reusing the result resource would make sense. Quote Link to comment https://forums.phpfreaks.com/topic/205251-loops-within-loops/#findComment-1074381 Share on other sites More sharing options...
theverychap Posted June 19, 2010 Share Posted June 19, 2010 Sorry, i read your code too quickly. You would be better off getting your results from the mysql table beforehand: $array = mysql_fetch_array($result); then instead of the while loop, use a foreach forreach ($array as $row) { ...do you row stuff... } Quote Link to comment https://forums.phpfreaks.com/topic/205251-loops-within-loops/#findComment-1074397 Share on other sites More sharing options...
nertskull Posted June 19, 2010 Author Share Posted June 19, 2010 Allright, so this is the code I have settled on. And it works. So I wanted to post it in case it helps anyone else. Also, just in case something is wrong that I don't know about. I ended up using the mysql_data_seek because it worked. The other suggestion of assigning it an array before hand didn't print out the right information. Thanks everyone for the help. Here's the code <table width="544" border="1" bordercolor="#000033"> <tr><th width="287">Date</th><th width="241">Weight</th></tr> <form name="InputWeight" method="POST" action="insert1_mult_go.php"> <?php $num_segments = $_POST['InputNumSelect']; for($i = 0; $i < $num_segments; $i++) {?> <p> <tr><td> <input name="Date[]"> </td><td><input name="Weight[]"> </td><td> <label> <select name="PersonSelect[]" id="PersonSelect"> <?php for ($j = mysql_num_rows($result) - 1; $j >= 0; $j--) { if (!mysql_data_seek($result, $j)) { echo "Cannot seek to row $j: " . mysql_error() . "\n"; continue; } if (!($row = mysql_fetch_assoc($result))) { continue; } echo "<option>" . $row['PersonID'] . ", " . $row['Name'] . ", " . $row['Nickname'] ."</option>\n"; } ?> </select> </label></td> </tr> </p> <?php } // EndFor formfields for number of segments needed?> <tr><td><input class="input_button" type="submit" name="submit" value="submit" /></td></tr> </form> </table> Quote Link to comment https://forums.phpfreaks.com/topic/205251-loops-within-loops/#findComment-1074431 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.