SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to generate a list of drop-down list box, and each list box gets it's options from MySQL, goes something like:

    PHP Code:
    <?php

    $sql 
    mysql_query ("SELECT firstname, lastname FROM table ORDER BY lastname") or die (mysql_error());

    $i=1;
    while (
    $i<="4")
    {

        echo 
    "<select name=\"name\">";
        while (
    $row mysql_fetch_array($sql))
            {
            
    extract ($row);
            echo 
    "<option value=\"$fname $lname\">$lname$fname</option>\n";
            }
        echo 
    "</select>";

    $i++;
    }

    ?>
    I get something like:


    list box 1: Elvis Presley, Bob Marley, Bill Gates, Steve Jobs

    list box 2:

    list box 3:

    list box 4:


    Basically I get 4 list boxes, which is what I want, but only the first list box has the correct options, the others have no options at all.
    Last edited by Robo; Apr 28, 2001 at 00:29.

  2. #2
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You only select the data once, so when you print out the options the first time, the script has already stepped through all of the rows. By the time it gets to the second iteration, it's already at the end of the DB, so the second WHILE does nothing.

    Try this:-

    PHP Code:
    <?php

    $sql 
    mysql_query ("SELECT firstname, lastname FROM table ORDER BY lastname") or die (mysql_error());

    $selector "<select name=\"name\">";
    while (
    $row mysql_fetch_array($sql))
            {
            
    extract ($row);
            
    $selector .="<option value=\"$fname $lname\">$lname$fname</option>\n";
            }
    $selector .="</select>";

    $i=1;
    while(
    $i<="4")
    {
    echo 
    $selector;
    $i++;
    }
    ?>
    May not be the cleanest code in the world, but I reckon it oughta do the trick. The script pulls the data for the list box into $selector and then prints it out 4 times.

    Hope this helps!
    Last edited by KMxRetro; Apr 28, 2001 at 01:40.

  3. #3
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that worked, now I got just a tiny problem...

    Here's the script:

    PHP Code:
    <?
    $form 
    .= "<tr>\n";

        
    $form .= "<td>\n"//start of column 2, listbox column
        
    $form .= "<select name=\"name$i\">\n";
        
    $form .= "<option value=\"\"></option>\n";
        while (
    $row mysql_fetch_array($sql))
            {
            
    extract ($row);
            
    $form .= "<option value=\"$fname $lname\">$lname$fname</option>\n";
            }
        
    $form .= "</select>\n";

    $form .= "</tr>\n\n";
            
    $i="1";
    while (
    $i<=$n)
            {
            echo 
    $form//now we actually print $form
            
    $i++;
            }
    ?>
    Notice this line:
    Code:
    <select name=\"name$i\">
    The $i is suppose to be the auto increasing number, so I'll end up with a list of select box with names "name1, name2, name3, name4" as it goes through the while loop.

    However, since $i isn't define till further down, $form doesn't catch $1's number, so all the select box just have the name "name" to all of them.

    I tried putting the line $i="1" at the top, but the names just all goes to "name1". I also tried defining a new variable, but ended up putting $form in a while loop, which defeats the purpose.

    So, is there anyway for each of the select box to have names that goes "name1, name2, name 3, name4"?

  4. #4
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you can try something like this:-

    Code:
    $var_name = "name";
        
        $form .= "<select name=\"$var_name\">\n";
      //...  
      
    $i++;
    $var_name.=$i;

    Hope this help
    Ngu I.P.
    Web Developer

  5. #5
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the input, unfortunately PHP still only spits out "name" to all of them...it replaced $varname with "name", but ignores the $varname .= $i in the while loop...

    Is there anyway to stop PHP from processing that variable till later on? Or solve the problem with the nested while loop? I don't mind if the script has to change quite a bit, as long as I get the name to increase in number everytime...

  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)
    Hi, I'm not sure if I got a good grasp on the preceding conversation, however, I think the logic of this code is what you are looking for:
    PHP Code:
    while($row mysql_fetch_array($sql))
    {
      
    // create a string for each option with the data from the current row
       // and store that string in an array
       
    extract($row);
       
    $options[] = "<option*value=\"$fname*$lname\">$lname,*$fname</option>\n";
    }

    $i 1;
    while (
    $i <= 4)
    // create four selects and insert the same options into each list
    // by looping through the options array for each select.
    {
       echo 
    "<select*name=\"name$i\">\n";
       foreach(
    $options as $option)
       {
          echo 
    $option;
       }
       echo 
    "</select>\n";
       
    $i++;


  7. #7
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you freaksid, that was what I wanted!


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
  •