Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8241963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:00:46+00:00 2026-06-07T21:00:46+00:00

I am trying to dynamically build a drop down menu using PHP. The idea

  • 0

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>';
    ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:00:47+00:00Added an answer on June 7, 2026 at 9:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build an associative array in PHP dynamically, and not quite getting
I am trying to dynamically build a list of admin actions using the get_actions()
I am trying to dynamically build a table on my page through ajax, using
So i am trying to build and prepare using PDO, a mysql query dynamically.
I have a javascript question. I'm trying to dynamically build a plugins string variable
When trying to dynamically load a Javascript file using jQuery I keep getting a
I'm trying to use the type(,,) function to dynamically build a module. The module
I build dynamically my HTML table from database like that: for (i = 0;
I am trying to build subprojects from my main Ant build script.. The build
I am trying to build a dynamically generated unordered list in the following format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.