Jump to content

making dropdown menu sticky


peppericious

Recommended Posts

Some forum users here gave me great help yesterday in working with dropdown menus. In fact, I need quite a few of these dropdown menus in several forms over several pages, so I created several simple arrays and made a function to create the dropdowns.

 

Here's one of my arrays:

 

<?php
$instruments = array(
'Bassoon', 'Cello', 'Clarinet', 'Double Bass', 'Flute', 'French Horn', 'Oboe', 'Percussion', 'Trombone', 'Trumpet', 'Tuba', 'Viola', 'Violin', 'Other'
);

 

Here's my function:

 

<?php
function create_dropdown($array_name, $array_item) {
  echo "<select name='$array_item'>\n";
  echo "<option value='select'>Select…</option>\n";
  foreach( $array_name as $v ) {
    echo "<option value='$v'>" . $v . "</option>\n";
  }
  echo "</select>\n";
}

 

... and anywhere I need a dropdown menu, I'm calling it like this:

 

<?php
create_dropdown($instruments, 'instrument');

 

The dropdown menus, however (in some fairly detailed forms for my local youth orchestra's site), need to be sticky.

 

How can I modify my function, above, so that the selected value will be retained if there are other form submission errors when the form is submitted?

 

I have tried endlessly today but to no avail... If you can help while I still have some hair left, I'd be greatly appreciative.

 

Link to comment
https://forums.phpfreaks.com/topic/258549-making-dropdown-menu-sticky/
Share on other sites

While you foreach through the values of the select menu itself you could add a third variable the function doesn't have to expect which is your "sticky variable"?

 

So whenever you call it and you need to stick something to the menu you would simply put that field into the function. Something like:

 

<?php

function create_dropdown($array_name, $array_item, $sticky_item = null) {

  echo "<select name='$array_item'>\n";
  echo "<option value='select'>Select…</option>\n";

  foreach( $array_name as $v ) {

    if($v == $sticky_item){

        echo "<option value='$v' selected>" . $v . "</option>\n";

    } else {

    echo "<option value='$v'>" . $v . "</option>\n";

    }

  }

  echo "</select>\n";

}

 

See what I'm getting at?

Functions should not echo content, they should return content (which you can then echo). Also, I would advise NOT creating the opening/closing select tags with the function - just create the options. The reason is if you want to add style/class/event handlers to the select statement you have to make the function very complex.

 

Also, it is quite typical that the VALUE of an option is not exactly the same as the LABEL. Therefore, I create these types of functions so I can pass an array with the values as the keys of the array and the values as the labels. Or use the values for both.

 

Here is what I would use for the function

function create_options($valuesAry, $useKeysAsValues=false, $selectedValue=false)
{
    $optionsList = '';
    foreach($valuesAry as $value => $label)
    {
        if(!$useKeysAsValues) { $value = $label; }
        $selected = ($selectedValue===$value) ? ' selected="selected"' : '';
        $optionsList = "<option value='{$value}'>{$label}</option>\n";
    }
    return $optionsList;
}

 

Usage:

<select name='instrument'>
<option value='select'>Select…</option>
<?php echo create_options($instruments, false, $selectedInstrument); ?>
</select>

 

So, let's say you get a list of values from the database and you need to use the primary key as the value. You would set up your array such that the primary ID is the key and the values are, well, the values. Examples:

//Don't use keys as values
$instruments = array('Bassoon', 'Cello', 'Clarinet');
create_options($instruments, false);

//Output:
// <option value='Bassoon'>Bassoon</option>
// <option value='Cello'>Cello</option>
// <option value='Clarinet'>Clarinet</option>


//Do use keys as values
$instruments = array(5 => 'Bassoon', 8=>'Cello', 13=>'Clarinet');
create_options($instruments, true);

//Output:
// <option value='5'>Bassoon</option>
// <option value='8'>Cello</option>
// <option value='13'>Clarinet</option>

I'm running into a problem....

 

Using the function as in this example:

 

//Don't use keys as values
$instruments = array('Bassoon', 'Cello', 'Clarinet');
create_options($instruments, false);

//Output:
// <option value='Bassoon'>Bassoon</option>
// <option value='Cello'>Cello</option>
// <option value='Clarinet'>Clarinet</option>

 

... only the last item in the array is being displayed in the menu.

 

Do I need to amend the function somehow?...

 

TIA

Per my signature

I do not always test the code I provide

 

Change

$optionsList = "<option value='{$value}'>{$label}</option>\n";

 

To

$optionsList .= "<option value='{$value}'>{$label}</option>\n";

Note the '.=' (dot equal) which appends the string to the current value of the variable

Change

$optionsList = "<option value='{$value}'>{$label}</option>\n";

 

To

$optionsList .= "<option value='{$value}'>{$label}</option>\n";

Note the '.=' (dot equal) which appends the string to the current value of the variable

 

Thank you... However, the menu selection is not being retained on submission of the form...

:0(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.