I am trying to dynamically build a drop down menu using PHP. The idea is: the elements are formed from a loop which calls and array. If the array element matches the data held in session then it adds the "selected" attribute to the tag, meaning that the page displays the previously selected option.

I have tried to include one complete set of code here, all the way from defining the variables from session data to echoing the HTML for the form element.

It doesn't currently work - the drop down menu appears, but is blank, and has no options. I've debugged it with ideone and it seemed to run successfully, and I can't see where I am going wrong, however this is my first PHP function! So I'm sure I've screwed it up somehow :)

Any help much appreciated.

    <?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>';
    ?>
share|improve this question
I'm guessing this is only part of the code but do you have session_start(); just after <?php in your original code? – Titanium Jul 14 '12 at 1:41
Ah yes - good point, should have mentioned that: yup I have that as an included php in all my pages. Thanks! I'll edit my post – Gideon Jul 14 '12 at 6:37
What do you get if you view the source of the generated page? – Luke Mills Jul 14 '12 at 7:32

2 Answers

Try this:

foreach ($dropdownoptions as $dropdownoption) {
  echo ($dropdownoption == $sv_02) ? "<option selected=\"selected\" value=\"$dropdownoption\">$dropdownoption</option>" : "<option value=\"$dropdownoption\">$dropdownoption</option>";}
share|improve this answer
That produces the same result as before unfortunately. Thanks for your thoughts though. Any other ideas? – Gideon Jul 14 '12 at 6:57
Maybe you can create a fiddle or include the rest of your code... – TGxANAHEiiMx Jul 14 '12 at 16:36
up vote 0 down vote accepted

This turned out to be a result of the fact I was using {smarty} tags to build my php, the code was as written but only worked when it was all included in one smarty tag, I'm not sure I understand why that should be the case but in any regard it was fixed by including it all in one tag.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.