Create a Dropdown menu in Php

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Create a Dropdown menu in Php

    I want to create a drop down menu in php for my local host and tried following, But using it The drop down menu appears, but is blank, and has no options.

    PHP Code:
      <?php
        session_start
    ();
        
    //if the session data has been set, then the variable $sv_02 is defined 
        //as the data held in the session under that name, otherwise it is blank
        
    if (isset($_SESSION['sv_02'])) {$sv_02=$_SESSION['sv_02'];} else {$sv_02="";}
        
    //define the array
        
    $dm_sv_02 = array('-Year','-2012','-2011','-2010','-2009');
        
    //create the function 
        
    function dropdown($dropdownoptions$session_data
        { 
        foreach(
    $dropdownoptions as $dropdownoption){
               if(
    $session_data == $dropdownoption){
                echo 
    '<option value="' $dropdownoption '" selected>' $dropdownoption '</option>';
               } else {
                echo 
    '<option value="' $dropdownoption '">' $dropdownoption '</option>';
               }
              }
        }
        
    //echo the HTML needed to create a drop down, and populate it with 
        //the function which should create the <option> elements
        
    echo '<select name="sv_02">';
        
    dropdown($dm_sv_02$sv_02);
        echo 
    '</select>';
        
    ?>
    *****
    Last edited by arnage; 12.04.13, 08:25.

    #2
    Here is a way without using function and with ternary.

    PHP Code:
    $arrays = array('one''two''three''four'); 

        echo 
    '<select name="set" title="Select">'."\n"
            foreach (
    $arrays as $array) { 
                echo 
    '<option value="'.$array.'"'.($session_data == $array ' selected="selected">' '>').$array.'</option>'."\n"
            } 
        echo 
    '</select>'."\n"
    <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

    Comment


      #3
      I know what you are trying to achieve, but some pointers before i give you a working snippet. Try making your codes more readable. All i see above are an unreadable bunch. Make your variables and array keys english words that can be easily read not "sv_02" which means nothing to anyone but yourself.

      Anyways I created a nifty little function that could help keep your code DRY. You can reuse this function to generate select dropdowns on any occassion as it supports it all. Although i wanted to include support for "optgroup" but i sense that would be an overkill. So enjoy.

      PHP Code:
      <?php

      // Start PHP session
      session_start();

      // Get the selected form field from the session data if saved
      $selected_year = isset($_SESSION['selected_year']) ? $_SESSION['selected_year'] : '';

      // Generate the years list to use for drop downs, you might
      // want to look into creating this list using something like PHP
      // "range" function instead of manually generating each year.
      $years_list = array(
          
      '' => 'Year',
          
      '2012' => '2012',
          
      '2011' =>  '2011',
          
      '2010' => '2010',
          
      // ...and the list goes on...
      );

      // Get the HTML years list and stuff.
      echo form_select('birthday_year'$years_list$selected_year);

      /**
       * Generate an HTML select list.
       *
       * <code>
       *    echo form_select('gender', array('m' => 'Male', 'f' => 'Female'), 'm');
       * </code>
       * 
       * @param string $name 
       * @param  array  $options    
       * @param  string|null $selected
       * @param array $attributes  
       * @return string                 
       */
      function form_select($name$options$selected false$attributes = array())
      {
          
      // If the options are not empty we'll iterate through each option and
          // add them to the select form that is being generated.
          
      if ( ! empty($options))
          {
              
      $name htmlentities($name);

              
      $attributes = ( ! empty($attributes) ? $attributes '');

              
      // The attributes of the select form will be checked and aggregated
              // after which they are put in the select tag, you can use this to
              // pass custom classes and id for the tag.
              
      if (is_array($attributes))
              {
                  
      $the_attributes '';

                  foreach (
      $attributes as $key => $value)
                  {
                      
      $the_attributes .= htmlentities($key).'='.htmlentities($value).' ';
                  }

                  
      $attributes $the_attributes;
              }

              
      $html '<select name="'.$name.'" '.$attributes.'>'.PHP_EOL;

              foreach (
      $options as $key => $value)
              {
                  
      $selected = ($key == $selected 'selected' '');

                  
      $html .= '<option value="'.htmlentities($key).'" '.$selected.'>'.htmlentities($value).'</option>'.PHP_EOL;
              }

              
      $html .= '</select>'.PHP_EOL;

              return 
      $html;
          }

          return 
      '';
      }
      Last edited by CreativityKills; 12.04.13, 19:15.

      Comment

      Working...
      X