populating dropdown list with column names?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cherryst152
    New Member
    • Nov 2007
    • 16

    #1

    populating dropdown list with column names?

    I keep getting problems with this. What i want to do is populate my dropdown list with the names of the columns in one of my database tables. Whatever i try to do i keep somehow filling it in with like 5 Array's or 5 Object's. Code is below and any help is greatly appreciated.

    $query33 = "SHOW COLUMNS FROM $January";
    $result33 = mysql_query($qu ery33);

    <select name='os1'>
    <?php
    if($result33){
    while($row = mysql_fetch_arr ay($result33)){
    echo "<option value='$row'>". $row."</option>";
    }
    }
    ?>
    </select>
  • clai83
    New Member
    • Dec 2007
    • 41

    #2
    Originally posted by cherryst152
    I keep getting problems with this. What i want to do is populate my dropdown list with the names of the columns in one of my database tables. Whatever i try to do i keep somehow filling it in with like 5 Array's or 5 Object's. Code is below and any help is greatly appreciated.

    $query33 = "SHOW COLUMNS FROM $January";
    $result33 = mysql_query($qu ery33);

    <select name='os1'>
    <?php
    if($result33){
    while($row = mysql_fetch_arr ay($result33)){
    echo "<option value='$row'>". $row."</option>";
    }
    }
    ?>
    </select>
    the $row that you extracted from the mysql result has a lot more information than you realize. It not only contains the column title, but also what kind of row it is, if its a PRIMARY KEY, or UNIQUE, a bunch of stuff. You need to change1 line to your code to make this work out the way you want it to.

    [PHP]
    echo "<option value=\"{$row['Field']}\">".$row['Field']."</option>";
    [/PHP]

    You may want to check out what the $row array includes as well so you know what you are working with.

    [PHP]
    $result = $mysql_query($q uery);
    while ($row = mysql_fetch_arr ay($result)) {
    print_r($row);
    }

    [/PHP]

    This will give you a good idea of what is inside the array so you know how to properly reference it next time

    hope this helps

    Comment

    • cherryst152
      New Member
      • Nov 2007
      • 16

      #3
      That worked perfectly. Thank you. One more thing though that should be easily solved, yet I am still have difficulty with....

      I want to take this a step further and display certain columns when put under a specified query... a random example would be easiest to see what i mean...

      January
      Day Ticket Payment Guest
      1 yes no yes
      2 no yes yes

      If i wanted the dropdown list to show only Payment when it is 'no' on Day 1.
      (The dropdown list would only have 'Payment' in it based only from the Day 1 row).

      Comment

      • clai83
        New Member
        • Dec 2007
        • 41

        #4
        Originally posted by cherryst152
        That worked perfectly. Thank you. One more thing though that should be easily solved, yet I am still have difficulty with....

        I want to take this a step further and display certain columns when put under a specified query... a random example would be easiest to see what i mean...

        January
        Day Ticket Payment Guest
        1 yes no yes
        2 no yes yes

        If i wanted the dropdown list to show only Payment when it is 'no' on Day 1.
        (The dropdown list would only have 'Payment' in it based only from the Day 1 row).
        You basically have to find out this information by sending a query to the database

        SELECT payment FROM table WHERE payment = 'no' AND day = 1

        then you test how many rows you got in your mysql result with the mysql_num_rows function. If it is equal to 1 then you have a match and you know that you show display it. If it is 0 then you shouldn't display it.

        something like that

        Comment

        • cherryst152
          New Member
          • Nov 2007
          • 16

          #5
          it should be one row no matter what if there are unique Days (such as Day 1).
          I'm just lookin to see if it's possible to display not all the column names at once, but rather specificy (maybe not even from a query--i dont really know) when a column has a certain value within a certain row and to select that certain column. maybe i shouldn't show all the columns at first and maybe there is another way to displaying column names with specifications? hope i'm not confusing too much.

          Comment

          Working...