3 Replies - 317 Views - Last Post: 16 March 2014 - 12:54 PM Rate Topic: -----

#1 cobbljock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 27-February 14

Dropdown menu code doesn't work when repeated

Posted 15 March 2014 - 04:31 PM

I'm populating a dropdown menu from a mysql database I created. I want to create multiple dropdown menus that all basically look the same, with the same information from the database, but I don't want to just ECHO the same dropdown menu multiple times, because I need the html IDs to be different. The script works for the first dropdown menu I create, but for reasons I don't understand, when I create the subsequent menus, the script doesn't work and menu options are not created from my database.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Select Page</title>
</head>
<body>
    <div align="center">Select a Team</div>
    <div align="center">
        <?php
        error_reporting(E_ALL);
        $con = //my connection code, I know it works
        if (!$con) {
        die('Could not connect: ' . mysqli_error());
        };
        $query = "SELECT Team FROM Teams ORDER BY Team ASC";
        $result= mysqli_query($con,$query);
        
        $dropdown1 = "<select id='list1'>";
            $dropdown1 .= "\r\n
            <option value='<Null>'>---Select a Team---</option>";
            while($row = mysqli_fetch_assoc($result)) {
            $dropdown1 .= "\r\n
            <option value='{$row[' team']}'>{$row['Team']}</option>";
            }
            $dropdown1 .= "\r\n
        </select>";
        
        $dropdown2 = "<select id='list2'>";
            $dropdown2 .= "\r\n
            <option value='<Null>'>---Select a Team---</option>";
            while($row = mysqli_fetch_assoc($result)) {
            $dropdown2 .= "\r\n
            <option value='{$row[' team']}'>{$row['Team']}</option>";
            }
            $dropdown2 .= "\r\n
        </select>";
        
        echo $dropdown1;
        echo $dropdown2;
        ?>
    </div>
    <br /><br />

</body>
</html>



I tried first reusing variables that were consistent ($query, for instance, doesn't change between select menus), using different variable names for each ($query1 and $query2 for the same code in the different select menu script parts), and finally I tried creating separate scripts for each menu, and connecting to my database each time. However, I still get the same problem. Can anyone tell me what I'm doing wrong? I'm new to PHP, but everything looks fine to my eyes. Obviously it's not. The code I posted above is my original code, before I tried the different variations I mentioned.

Also, even though $row doesn't change between menus, I did try making separate $row variables ($row1 and $row2) for each menu.

Is This A Good Question/Topic? 0
  • +

Replies To: Dropdown menu code doesn't work when repeated

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3560
  • View blog
  • Posts: 10,356
  • Joined: 08-June 10

Re: Dropdown menu code doesn't work when repeated

Posted 15 March 2014 - 04:48 PM

Iím pretty sure to have read this question an hour ago somewhere ... same reason though, when you finish the first while() loop there are no more results to fetch for the second.

you would have to save the results in an array first and then you can iterate over the array as many times as you want.

although I have to say that the HTML created in the while loop looks exactly the same to me. so just do the loop once and save the HTML code in a variable and re-use that.
Was This Post Helpful? 1
  • +
  • -

#3 cobbljock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 27-February 14

Re: Dropdown menu code doesn't work when repeated

Posted 16 March 2014 - 08:21 AM

Ahh. Actually, I should have posted the code where I did separate variables, because I found a tiny error, and that's why my later revisions didn't work. So I got the problem fixed, but your comment has me curious. When a query's results are used in a mysqli_fetch_assoc, they are "used" and therefore destroyed or something? I've looked all over online about the nature of mysqli query results, but I have yet to find anything that makes much sense to me, so my question might be pretty dumb.

AS far as putting the loop in an HTML, is that possible, since I'm creating the $dropdown1 variable through concatenation, and I need each variable to have a different ID (does that make sense?), and the $dropdown1 variable is in the loop code? I know you could probably recycle the $dropdown1 variable, but I've heard that's not very good form.

This post has been edited by Dormilich: 16 March 2014 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3560
  • View blog
  • Posts: 10,356
  • Joined: 08-June 10

Re: Dropdown menu code doesn't work when repeated

Posted 16 March 2014 - 12:54 PM

Quote

When a query's results are used in a mysqli_fetch_assoc, they are "used" and therefore destroyed or something?

something like that, MySQL fetches the results stores that in RAM and every call to fetch_assoc() gets one row from MySQL (which then GCs the data because it needs the space for the next query). since you overwrite the result variable in each loop cycle, the values are indeed destroyed.

at least thatís how Iíd explain it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1