SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selecting a value from dynamically populated drop down

    I am trying to set the selected item in a dynamically populted drop down as follows but it is always selecting the last element in the drop down. Any ideas what I am doing wrong? Please note I have just hard coded it for now to a value of 2. The value I will need to compare it to is a variable called $companyId

    Code:
    <?php
    		echo "<select name='companies'>";
    		echo "<option value='-1' selected>Select a company</option>";
    
    		while ($r = mysql_fetch_array($result)) {
    
    			$id=$r["id"];				
    			$companyName = $r["companyName"];				
    						
    			echo " <option value='$id' if ($id == '2') { selected }; >$companyName</option>";
    		}
    
    		echo "</select>\n";
    				
    
    		?>
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
    while ($r mysql_fetch_array($result)) {
      
    $id=$r["id"];                
      
    $companyName $r["companyName"];                
      echo 
    " <option value='$id' ";
      if (
    $id == '2') echo " selected ";
      echo 
    ">$companyName</option>";


  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a million quido - that works a treat!
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk


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
  •