SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: while looping

  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool while looping

    I'm dynamically building a form and populating some data fields from a db.
    However, my current code works for populating the AgtCode field ONLY FOR THE FIRST ROW that is built, and not for the subsequent rows. I think it's because of the placement of the while loop and the fact that I'm iterating through an array, but I am not sure how to set up my loop construct. Do I need to use mysql_fetch_object instead at a different point in the code?


    PHP Code:
    $numRecords=$_POST['numRecords'];
    $counter="0";
    while (
    $counter $numRecords) {
    $status = ($counter) ? 'Odd' 'Even';
    if(
    $status=='Even') {
    //Removed a bunch of table code here for simplification     
         
    print "</td><td><select name=\"AgtCode\">";
          while (
    $r mysql_fetch_array($result))
             {
               
    $value $r["AgtCode"];
               echo 
    "<option value=" $value " >" $value "</option>";
           }
         </
    td></tr>";
    $counter += 1;
    } else {
    //Removed a bunch of table code here for simplification     
         print "
    <tr><td><select name=\"AgtCode\">";
          while (
    $r mysql_fetch_array($result))
             {
               
    $value $r["AgtCode"];
               echo 
    "<option value=" $value " >" $value "</option>";
           }
         </
    td></tr>";
    $counter += 1;
    }
    }
    echo "
    </table>"; 
    So, the second time through the while($counter) loop, the AgtCode field does not populate. Can somebody help me out? Thanks!
    Last edited by kbdaemon; Nov 10, 2006 at 09:42.

  2. #2
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can we see your SQL query?
    Garcia

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    re:

    PHP Code:
    $cnxn mysql_connect(DATABASE_SERVERDATABASE_USRDATABASE_PWD) OR DIE("DB connection failed: " mysqli_connect_error());
    $dbselect mysql_select_db(DATABASE_NAME) OR DIE ("DB selection failed");
    //define the sql statement
    $sql="SELECT AgtCode, AgtName FROM agt_info";
    //execute the query, handle any errors
    $result mysql_query($sql);
    if (!
    $result) {
       
    $message  'Invalid query: ' mysql_error() . "\n";
       
    $message .= 'Whole query: ' $sql;
       die(
    $message);


  4. #4
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And I suppose you have already checked that the query returns more than one record?
    Garcia

  5. #5
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now that you have trimmed your code, its more obvious that you only need one of the two loops, like you stated. I also noticed that you should be using mysql_fetch_assoc instead of mysql_fetch_array, since you are referencing the array by key name.
    Garcia

  6. #6
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure we're on the same page...The first time through, the options for the select are fully populated. Yet, on subsequent passes the select contains absolutely nothing. So the sql statement is/has been working just fine.

    The sql statement should only have to be called one time, and then I should be able to work with the array data.

    changing to mysql_fetch_assoc makes no difference to the bigger problem, but I do appreciate you pointing that out.

    I tried using

    PHP Code:
    reset($r
    immediately after the while loop ends, but that didn't work.

  7. #7
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Solution, but not what I was trying to do...

    By performing the query directly in front of each while loop I can populate the drop down lists, no matter how many rows (table rows, not talking about the size of the array returned by the query) there are:

    PHP Code:
    $result mysql_query($sql);
    while (
    $r mysql_fetch_assoc($result))
    {
      
    $value $r["AgtCode"];
      echo 
    "<option value=" $value " >" $value "</option>";

    This isn't what I wanted to do, because it seems unnecessary to use a query when I should just be able to work with the array...Any ideas???
    Please see new question:
    need a new viewpoint for array built from query
    Last edited by kbdaemon; Nov 10, 2006 at 13:53. Reason: clarification


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •