SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting enum type to make dropdown

    Hi guys, as mentioned, I want to get the types from an enum to create a dropdown based on those values.

    for e.g, if i have
    status enum('New','Young','Old','Ancient') NOT NULL default 'New'
    My dropdown would be :

    <option value="New">New</option>
    <option value="Young">Young</option>...

    I just need a way to access the enum types. Any ideas?

    thanks in advance
    asT.

  2. #2
    SitePoint Zealot LiamW's Avatar
    Join Date
    Mar 2003
    Location
    Hamilton, Ontario
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're trying to make a select box correct?

    Something like this would do.

    PHP Code:
    while ($row mysql_fetch_array($result))
            {
                
    $values= ['status']
                echo 
    "<option value=\"$values\">$values</option>";
            } 

    I believe that should do it assuming you know how to do the query part, if not just ask.

  3. #3
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since these are the only options for the field, I would hard code them in an area where you can make any changes (should you change the field definition) once, and it will make the changes wherever the dropdown is used in your scripts.
    John

  4. #4
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow, you guys are quick at replying.

    Liam, I tihnk you misunderstood the Q. I dont need to pull the values submitted, but the 'types' that the enum allows.

    John, I was just wondering how to do it coz in case I change the enum('stuff '), I dont wanna go around changing it in my codes.

    For now, I already have it handcoded where I would change it only once.

    thanks for the suggestion though.

    any other ideas?

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This would work fine.
    Code:
    <?php
    $db = mysql_connect("localhost", "root", "" ) or DIE(mysql_error());
    mysql_select_db("test" );
    $q = mysql_query("DESCRIBE food status" );
    $field = mysql_result($q, 0, 'Type');
    preg_match_all("/\'[^\']+\'+/", $field, $temp);
    $options = $temp[0];
    
    ?>

  6. #6
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers freddy, that did the trick. This query helped.
    DESCRIBE tbl_name field_name
    i wonder where you got your nick from...heheheh


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
  •