SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Dropdown menu in Php

    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>';
        
    ?>
    <snip />
    Last edited by cpradio; Apr 11, 2013 at 05:15. Reason: Please wait 90 days for a real signature

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I tested your code and it works fine for me. It outputs (I added line breaks for readability):
    HTML Code:
    <select name="sv_02">
    <option value="-Year">-Year</option>
    <option value="-2012">-2012</option>
    <option value="-2011">-2011</option>
    <option value="-2010">-2010</option>
    <option value="-2009">-2009</option>
    </select>
    Denny Schlesinger
    web services

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Works fine for me.

    Alternative (taking the echo out of the function):

    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)
    {
         
    $str='';
        foreach(
    $dropdownoptions as $dropdownoption){
            if(
    $session_data == $dropdownoption){
                
    $str .= '<option value="' $dropdownoption '" selected>' $dropdownoption '</option>';
            } else {
                
    $str .= '<option value="' $dropdownoption '">' $dropdownoption '</option>';
            }
            
    $str .= "\n";
        }
        return 
    $str;
    }

    //echo the HTML needed to create a drop down, and populate it with 
    //the function which should create the <option> elements
    ?>

    <form>
    <select name="sv_02">
    <?php
    echo dropdown($dm_sv_02$sv_02);
    ?>
    </select>
    </form>
    But ... is this really just a SPAM post? Otherwise why have you included a link in your post?
    Ian Anderson
    www.siteguru.co.uk

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,054
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by siteguru View Post
    But ... is this really just a SPAM post? Otherwise why have you included a link in your post?
    Taken care of, in the future, please feel free to use the Report button (red flag) on the offending post.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    I didn't want to waste moderator time unnecessarily by reporting, so I was giving the OP the benefit of the doubt by asking first.
    Ian Anderson
    www.siteguru.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
  •