SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Sausalito, Ca
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at this:

    $query = mysql_query("select * from project order by clientname")
    or die("invalid query - \$query - projman.php");
    $result = mysql_fetch_array($query);

    // then in my form
    <select>
    do {
    <option value=""><? echo $result['clientname']?>
    }
    while ($result=mysql_fetch_array($query));
    </select>

    <select>
    do {
    <option value=""><? echo $result['projectid']?>
    }
    while ($result=mysql_fetch_array($query));
    </select>

    // I should get two select lists populated by the appropriate values from my query. But no, it makes the first list for me, but the second one is empty. If I make two different queries it works:

    $query = mysql_query("select * from project order by clientname")
    or die("invalid query - \$query - projman.php");
    $result = mysql_fetch_array($query);

    $queryone = mysql_query("select * from project order by clientname")
    or die("invalid query - \$queryone - projman.php");
    $resultone = mysql_fetch_array($queryone);

    // then in my form
    <select>
    do {
    <option value=""><? echo $result['clientname']?>
    }
    while ($result=mysql_fetch_array($query));
    </select>

    <select>
    do {
    <option value=""><? echo $resultone['projectid']?>
    }
    while ($result=mysql_fetch_array($queryone));
    </select>

    // this seems stupid and redundant. Not only that, but I don't recall ever running into this problem before. I should be able to do it the way the first example shows, right? Or have I missed something? It's driving me insane.

  2. #2
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your problem is in your loops.

    You're saying

    While there are MySQL results left make this dropdown list
    (it does it, and when it reaches the end of the result set it stops)

    Then you say it again.
    (checks the result set, sees that its at the end already, does nothing)

    Since the first loop has already gone through all your results you have no results left - so thats why the second one isn't working.

    A solution would be to input both needed fields into 2 different arrays in a loop like you have now.

    Then loop through each array individually to generate your select list.

    Or of course you could use 2 queries.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Sausalito, Ca
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suspected that might be the problem, so I tried to use the reset() function but got an error.

    do {
    <option value=""><? echo $result['clientname']?>
    }
    while ($result=mysql_fetch_array($query));
    //using,
    reset($result=mysql_fetch_array($query));
    //or,
    reset($result);

    // produces the following error
    Warning: Variable passed to reset() is not an array or object in filename on line 132

    I 've never used reset() before, am I using it incorrectly?

  4. #4
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Try this...

    Try this man....

    Code:
    $query = mysql_query("select * from project order by clientname") 
    or die("invalid query - \$query - projman.php"); 
    $result = mysql_fetch_array($query);
    $resultsub = mysql_fetch_array($query);
    
    // then in my form 
    <select> 
    do { 
    <option value=""><? echo $result['clientname']?> 
    } 
    while ($result=mysql_fetch_array($query)); 
    </select> 
    
    <select> 
    do { 
    <option value=""><? echo $result['projectid']?> 
    } 
    while ($resultsub=mysql_fetch_array($query)); 
    </select>
    That should do the trick.

    Not sure about resets, I'll look into it.
    Hope this helps,

  5. #5
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Or...

    Or maybe even this to save storing two arrays (shouldn't make much difference...)


    Code:
    $query = mysql_query("select * from project order by clientname") 
    or die("invalid query - \$query - projman.php"); 
    $result = mysql_fetch_array($query); 
    
    // then in my form 
    <select> 
    do { 
    <option value=""><? echo $result['clientname']?> 
    } 
    while ($result=mysql_fetch_array($query)); 
    reset[$result];
    </select>
    
    <select> 
    do { 
    <option value=""><? echo $result['projectid']?> 
    } 
    while ($result=mysql_fetch_array($query)); 
    </select>
    reset[$result] *SHOULD* set the internal pointer back to the start of the array, giving you output on the second dropdown.

    Give it a try and see if it works...

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    eli - regarding resetting the array pointer of $result. Well, $result is not an array! In fact $result is an int. It is an identifyer of a result set. The mysql functions in PHP, things such as mysql_fetch_array encapsulate code that make use of the MySQL C language API to communicate between php and the MySQL server. What is a result set? I don't know what it is, but it isn't an array and its not really a type of data either in the sense of being an array or string or int.

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Sausalito, Ca
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so it's not actually an array? Even though it acts like one? I'll admit that I am just a beginner in the scope of programming, as I come from a graphics background which led into html, which led into server side scripting. So if it's not an array, then can you explain the following?

    $query = mysql_query("select * from sometable");
    $result = mysql_fetch_array($query);

    echo $result;
    // returns the following
    Array

    echo $query;
    //returns
    Resource id #2

    I think that I see your point in here somewhere when I echo $query, but doesn't populating $result with mysql_fetch_array($query); make $result an array?

  8. #8
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My apologies. I got confused. According to your code:

    $query = mysql_query("select * from sometable");
    $query <- will be assigned an int representing the MySQL result set.

    $result = mysql_fetch_array($query);
    $result <- will be assigned the next row in the result set. The row will be made into an associative array with a name/value pair for each field of the slq result set.

    So yes, $result is an array. But it only ever holds name/value pair elements of one row from the MySQL result set. So reseting the array pointer reset($result); will just move the pointer to the first element of $result which will be the first field of the current row from the result set.

    hehe - does that make any sense? And by the way, artistic people often make good programmers (IMHO) they often can see the broader design or pattern in a problem/solution.

    I don't think it is a coincidence that there are so many left handed programmers! Hey - I've been meaning to start a poll on this for ages. I might do so out of curiousity.


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
  •