Jump to content

Loops within Loops


nertskull

Recommended Posts

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]

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

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>

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.