Results 1 to 2 of 2

Thread: MySQL Fields In A Drop Down Option Form

  1. #1
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool MySQL Fields In A Drop Down Option Form

    Lets say I have the following table in mysql....

    Field Name - |db1|db2|db3|db4|db5|
    Data - |db1|db2|db3|db4|db5|

    Lets say I want to put all those data entries into a drop down list...

    PHP Code:
    while ($row mysql_fetch_array($result)){
    echo 
    '<select size="1" name="db">';
    echo 
    '<option value="'.$row['db1'].'">'.$row['db1'].'</option>';
    echo 
    '<option value="'.$row['db2'].'">'.$row['db2'].'</option>';
    echo 
    '<option value="'.$row['db3'].'">'.$row['db3'].'</option>';
    echo 
    '<option value="'.$row['db4'].'">'.$row['db4'].'</option>';
    echo 
    '<option value="'.$row['db5'].'">'.$row['db5'].'</option>';
    echo 
    '</select>'
    My question is, can I do this a simpler way?
    e.g.

    Field Name - |db1|db2|db3|db4|db5|
    Data - |db1|db2|

    So if only db1 and db2 has data, this means the drop down box will only have 2 options, but will still be fairly big in height!!

    another e.g.

    Field Name - |db1|db2|db3|db4|db5|db6|db7|db8|db9|db10|
    Data - |db1|db2|db3|db4|db5|db6|db7|db8|db9|db10|

    In this example it would get quite teadious having to script in each field into the drop down list. I'm expecting to use no more than 20db fields in total. This means I could either allow for 20 options in the drop down, or I'd like for there to be a way to only do a drop down list for ones that exist.

    I do hope someone knows what I mean!!

    Here is what I have so far but it outputs nothing at all in the drop down.
    PHP Code:
    while ($row mysql_fetch_array($result)){
      echo 
    '<select size="1" name="db">';
      
    $numrows mysql_num_rows($result);
      for(
    $i 1$i $numrows$i++){
        echo 
    '<option value="'.$row["db$i"].'">'.$row["db$i"].'</option>';
      }
      echo 
    '</select>';

    Thanks in advance.

    Smithster.
    Last edited by smithster; 12-23-2007 at 06:41 PM.

  2. #2
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    That should be simple. Put an if/else statement in the loop.

    PHP Code:
    <?php

    $table 
    "your Table";
    $rows MySQL_Query("SELECT * FROM $table");

    print 
    "<select name='something'>";

    //Get a row
    while($row MySQL_Fetch_array($rows)){

    //get a feild
    for($i=0;$i<count($row);$i++){

    //Grab an element in the array
    $array next($row); //A function I just learned :)

    //make sure there is something in it
    if($array != ""){

    //echo it
    echo "<option>$array</option>";

    }

    }

    }
    print 
    "</select>";
    ?>
    I haven't tested it yet. . . But it's a start.

    EDIT: It works!
    Last edited by Jas; 12-24-2007 at 04:14 AM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

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
  •