Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
problem with nested loops
Old 04-26-2005, 03:38 PM problem with nested loops
Junior Talker

Posts: 3
Trades: 0
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!
ptaylor2005 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-26-2005, 04:12 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
What that will give you is
HTML Code:
<input type="text" name="wlocation[]" size="28" maxlength="185" style="font-size:10px"
value="row1" value="row2" value="row3" /><br />
Instead, put the while loop around everything:
PHP Code:
while($wl mysql_fetch_array($sql)){
echo 
"<input type=\"text\" name=\"wlocation[]\" size=\"28\" maxlength=\"185\" style=\"font-size: 10px\"\n";
 echo 
"value=\"" .$wl[write_region]."\"\n";
 echo 
" /><br />\n";

That will give you 3 text boxes, with row1 in the first, row2 in the second etc.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 04-26-2005, 04:18 PM
Junior Talker

Posts: 3
Trades: 0
Hi,
Thanks! That works nicely...the only catch is that I need to have it write out 3 textboxes total, regardless of how many records are returned by the query, even if there are no records returned.
Is there any easy way to do that, perhaps with an outside loop that counts up to three?

Thanks.
ptaylor2005 is offline
Reply With Quote
View Public Profile
 
Old 04-26-2005, 05:37 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
How about ::
PHP Code:
for($n=0$n<3$n++) {
  echo 
"<input type=\"text\" name=\"wlocation[]\" size=\"28\" maxlength=\"185\" style=\"font-size: 10px\"\n";
  if(
$wl mysql_fetch_array($sql)){
    echo 
"value=\"" .$wl[write_region]."\"\n";
  }
  echo 
" /><br />\n";

This is almost exactly what you had in the first place, but with a simple if() instead of a while in the middle. That way it picks off one result each time if there is one available rather than doing them all the first time around.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 04-27-2005, 04:20 PM
Junior Talker

Posts: 3
Trades: 0
Thanks for the help! That worked well!
ptaylor2005 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to problem with nested loops
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.83063 seconds with 12 queries