Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 26th, 2005, 01:21 PM
Registered User
 
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with nested loops

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!
 
Old April 26th, 2005, 06:54 PM
Registered User
 
Join Date: Apr 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Wade
Default

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.

 
Old April 27th, 2005, 04:47 AM
Authorized User
 
Join Date: Apr 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jmukesh Send a message via Yahoo to jmukesh
Default

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.
 
Old April 27th, 2005, 02:55 PM
Registered User
 
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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";
  }
 
Old April 27th, 2005, 07:05 PM
Registered User
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Loops in SQL Query values from table/set miamikk SQL Server 2000 1 August 31st, 2007 12:35 AM
break for-each loops, or limit amount of loops warhero XSLT 2 July 4th, 2007 02:18 AM
problem with arrays/loops jfern Javascript How-To 4 November 7th, 2006 10:29 AM
weird program flow with nested loops zayasv Intro Programming 2 November 17th, 2005 06:19 AM
While loops and For loops in XSLT spencer.clark XSLT 1 August 5th, 2005 09:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.