SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2004
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to place the 'selected' attribute in an option tag ?

    Hi all,

    small problem. I use the below code to generate a drop down list, built from a database table:

    PHP Code:
    <?php 
    //Display list of all types
    $query_vestigingen="SELECT vestiging_id, vestiging FROM vestigingen WHERE dealer_id = '$dealer_id'";
                    
    $result_vestigingen mysql_query($query_vestigingen);
    echo 
    "<select name=vestiging_id value=''>vestiging</option>";
                    
    while(
    $nt=mysql_fetch_array($result_vestigingen)){
    echo 
    "<option value=$nt[vestiging_id]>$nt[vestiging]</option>";
    }
    echo 
    "</select>"
    ?>
    This works fine. My question is, how can I detect the value which is actually mentioned in the database and have the drop down automatically display the right value ? I have a page where I need to update the values for a specific car, so for the update page, I need the dropdown list to show the saved data which is in the db.

    I tried like this, but I can't make it work, I'm stuck I guess with the ' and the "

    PHP Code:
    echo "<option value=$nt[vestiging_id] if ($nt[vestiging_id] == $vestiging_id_db) echo 'selected'>$nt[vestiging]</option>"
    where $vestiging_id_db is the value which is mentioned in the db. So I would like the drop down box to display the value $vestiging_id_db.

    Any ideas why it doesn't work?

    Kind regards,
    Michel

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will work for you:
    PHP Code:
        //Display list of all types
        
    $query_vestigingen="SELECT vestiging_id, vestiging FROM vestigingen WHERE dealer_id = '$dealer_id'";
        
    $result_vestigingen mysql_query($query_vestigingen);
        
    //lets suppose your selecting id is 2;
        
    $selid 2;
        echo 
    "<select name=vestiging_id value=''>vestiging</option>";
        while(
    $nt=mysql_fetch_array($result_vestigingen)){
            if(
    $selid == $nt[vestiging_id]){
                echo 
    "<option value=\"$nt[vestiging_id]\" selected>$nt[vestiging]</option>\n";
            }
            else{
                echo 
    "<option value=\"$nt[vestiging_id]\">$nt[vestiging]</option>\n";
            }
        }
        echo 
    "</select>"
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just use single quotes so you don't need to escape double quotes. Unnecessary escaping causes cancer.

    You may not be seeing the preselected value if you are hitting refresh in Firefox. It remembers your last "entry" between page requests. You need to hit enter from the address bar to request the page again for the selected to take effect.


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
  •