SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selecting elements in dropdown menus

    Hi! I have three <select> elements that I use to enter data about day, month and year in a database. In a specific page of the site the user has the possibility to edit the date, and I would like the page to automatically select the correct elements of the menu based on the information in the database (so that, if the date inserted is 01-02-2011, the third <select> element would have the "2011" element already selected).

    I was thinking about the following solution:

    PHP Code:
    //
    // retrieve information from the database and save it in the variables $day, $month and $year
    //

    if ($year == '2011') {
      echo 
    '<select>
                <option value="2011" selected="selected">2011</option>
                <option value="2012">2012</option>
              </select>'
    ;
    else if (
    $year == '2012') {
      echo 
    '<select>
                <option value="2011">2011</option>
                <option value="2012" selected="selected">2012</option>
              </select>'
    ;

    but is there a better way to do this?

  2. #2
    SitePoint Member centrex's Avatar
    Join Date
    Feb 2011
    Location
    San Diego
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had a similar requirement not too long ago; this is how I tackled it. I'm still a novice so there may be other/better options.

    PHP Code:
    $my_year '2008';
    $all_years = array('2005''2006''2007''2008''2009''2010''2011''2012');

    $options NULL;

    foreach(
    $all_years as $year) {
        if(
    $year === $my_year) {
            
    $selected ' selected="selected"';
        } else {
            
    $selected NULL;
        }
        
    $options .= '<option value="' $year '"' $selected '>' $year '</option>' PHP_EOL;

    Output:
    PHP Code:
        <select>
        <?php echo $options?>
        </select>
    Result:
    HTML Code:
    <select>
        <option value="2005">2005</option>
        <option value="2006">2006</option>
        <option value="2007">2007</option>
        <option value="2008" selected="selected">2008</option>
        <option value="2009">2009</option>
        <option value="2010">2010</option>
        <option value="2011">2011</option>
        <option value="2012">2012</option>
    </select>

  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)
    Centrex is pretty well on the money, but as NULL gets cast to an empty string when being used in building $options you might as well make it an empty string to begin with.

    The ternary operator is also handy in this situation to have a more compact line.
    PHP Code:
    foreach($all_years as $year) {
      
    $selected = ($year == $my_year) ? ' selected="selected"' '';
     
      
    //...


  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Here is a function I found years ago and used for a long while. It goes to the db and gets elements for your droplist and matches to the one you want.
    PHP Code:
    function ListboxMatch($size$name$query$matchtothis) { 
    mysql_select_db($dbname,$mysql_link);

    $qry mysql_query($query,$mysql_link); 
        if (
    mysql_num_rows($qry) > 0
            { echo 
    "<SELECT SIZE='".$size."' NAME='".$name."'>" PHP_EOL;
                 while (list(
    $value$text) = mysql_fetch_row($qry)) 
                    { echo 
    "\t<OPTION VALUE='".$value."'";
                         if (
    $value == $matchtothis
                        { echo 
    " SELECTED"; } 
                                            echo 
    ">".$text."</option>" PHP_EOL
                    } echo 
    "</SELECT>" PHP_EOL;
             } else echo 
    "No data in listbox" PHP_EOL;
         
    mysql_free_result($qry); 

    No guarantee all these mysql_* functions are correct or still exist - but might give you some ideas - it clearly needs some cleaning up (all caps for html elements, wtf?), but still it should work.
    PHP Code:
    $this_person 7// from get or post or session etc

    // get 2 things, the value and the text you want to display
    $query "select id, name from people order by name";

    // it echoes everything to the screen when called, so put this 
    // line in the correct place
    ListboxMatch(1'people'$query$this_person); 
    I remember taking this idea and improving it somewhat but I cannot remember where the code is.

  5. #5
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've used centrex's solution and improved it with cranial-bore's suggestions, and it works perfectly. Thank you


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
  •