Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2010
    Location
    Montreal, CA
    Posts
    32

    Unanswered: Default Value for PHP Drop Down List Populated By MySQL

    Hi,

    I am in the process of creating a generic drop down list function.
    Its values will be read from a MySQL database table.

    My issue is with the default value.
    I was hoping to have my table designed with a boolean column to determine the default record (default=1, all others set to 0). How can I implement this in my PHP code without doing 2 queries on the table and ultimately getting my default value as the 1st item in the drop down list? i.e. I would like my drop down list item order to be conserved regardless of what value I set as the default.

    Thanks

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    use an order by that includes the use first flag

    eg
    select my,column,list from mytable
    order by isdefault DESC, my, sort, order ASC
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Feb 2010
    Location
    Montreal, CA
    Posts
    32
    Quote Originally Posted by healdem View Post
    use an order by that includes the use first flag

    eg
    select my,column,list from mytable
    order by isdefault DESC, my, sort, order ASC
    select my,
    column,
    list
    from mytable
    order by isdefault DESC,
    my,
    sort,
    order ASC


    I'm not sure I understand the explanation.

    Just to clarify, if I had a list:
    A,B,C,D

    and B is default, I would like to maintain the order:
    A,B,C,D

    rather than have:
    B,A,B,C

    Thanks again

  4. #4
    Join Date
    May 2008
    Posts
    277
    Select your option data as normal:
    Code:
    select
        value,
        is_default
    from my_table
    order by value
    Now in your php code:
    PHP Code:
    foreach ($rows as $row) {
        if (isset(
    $user_input)) {
            
    $selected $user_input == $row['value'] ? ' selected="selected"' '';
        } 
        else {
            
    $selected $row['is_default'] ? ' selected="selected"' '';
        }

        echo 
    "<option value=\"$row[value]\"$selected>$row[value]</option>";


  5. #5
    Join Date
    Feb 2010
    Location
    Montreal, CA
    Posts
    32
    Quote Originally Posted by futurity View Post
    Select your option data as normal:
    Code:
    select
        value,
        is_default
    from my_table
    order by value
    Now in your php code:
    PHP Code:
    foreach ($rows as $row) {
        if (isset(
    $user_input)) {
            
    $selected $user_input == $row['value'] ? ' selected="selected"' '';
        } 
        else {
            
    $selected $row['is_default'] ? ' selected="selected"' '';
        }

        echo 
    "<option value=\"$row[value]\"$selected>$row[value]</option>";

    awesome, works great.. thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •