|
Subject:
|
problem with nested loops
|
|
Posted By:
|
ptaylor2005
|
Post Date:
|
4/26/2005 1:21:49 PM
|
Hi, Having a problem with a nested loop outputting to html. Trying to dynamically write out three textboxes onto the page, and populate them with the resultset of a simple query. This is the query:
$sql = mysql_query("Select write_region From resume_locations Where resume_id = '$res_id'");
The three textboxes are then written out via the following: for($n=0;$n<3;$n++) { echo "<input type=\"text\" name=\"wlocation[]\" size=\"28\" maxlength=\"185\" style=\"font-size: 10px\"\n";
I then tried to use another loop to populate the textbox values with the results of the query. Please note, there should always be three textboxes, even if the query returns zero rows. That way the user can enter data into the textboxes. So the next loop is:
while($wl = mysql_fetch_array($sql)){ echo "value=\"" .$wl[write_region]."\"\n"; }
Then close the textbox:
echo " /><br />\n"; }
Of course what's happening is that only the first row of data from the query is being written out, the other textboxes are empty, although in this particular query there should be 2 rows of data.
Any help would be gratefully appreciated. Thanks!
|
|
Reply By:
|
Wade
|
Reply Date:
|
4/26/2005 6:54:57 PM
|
Try
while($wl = mysql_fetch_assoc($sql)){ echo "value=\"" .$wl[write_region]."\"\n"; }
Instead of:
while($wl = mysql_fetch_array($sql)){ echo "value=\"" .$wl[write_region]."\"\n"; }
Thats all I can think of.
|
|
Reply By:
|
jmukesh
|
Reply Date:
|
4/27/2005 4:47:50 AM
|
Hi <?
$sql = mysql_query("Select write_region From resume_locations Where resume_id = '$res_id'"); $currentRecord = 1 ; for($n=1;$n<=3;$n++) {
?> <input type="text" name="wlocation[]" size="28" maxlength="185" style="font-size: 10px"
<? if( $currentRecord <= mysql_num_rows($sql)){ $wl = mysql_fetch_array($sql); echo " value=\"$wl[write_region]\" "; } ?> >
<? $currentRecord++; } ?>
This will solve your problem. 
|
|
Reply By:
|
ptaylor2005
|
Reply Date:
|
4/27/2005 2:55:28 PM
|
Thanks for the replies. I managed to get it going with:
$sql = mysql_query("Select write_region From smi_resume_locations Where resume_id = '$res_id'");
for ($q=0;$q<3;$q++){ if ($row = @mysql_fetch_array($sql)){ $val = $row['write_region']; } else { $val = ""; } echo "<input type=\"text\" name=\"wlocation[]\" size=\"28\" maxlength=\"185\" style=\"font-size: 10px\" value=\"$val\" /><br />\n"; }
|
|
Reply By:
|
jmrdeuce32
|
Reply Date:
|
4/27/2005 7:05:05 PM
|
You could just make the three text areas with html, and use array from the result to assign the values to the corresponding text area.
|