siteIcon

Tech Novice Tools

PHP Apps

burgerIcon

Marco Poloexplorer

Stage 3:
Function-Generated,
Array-Based Select Menus via PHP

About the app

Creating drop-down menus can be a pain when there are lots of options. Wouldn't it be nice if we could use a function in PHP to create the menu for us?

That's the whole point of this app.

First, we created a list of gifts and stored them in a motel-like data structure called an 'array.'

A simple array in PHP:

Observe the syntax to create an array in PHP. The 'array' PHP function stores comma-separated elements in each 'index' (room number) of the 'motel.' It is (almost?) universal that the first index is zero rather than one. This is known as 'zero-based indexing'.

                                
<?php          
    $arr = array("Partridge in a Pear Tree", "Turtle Dove", "French Hen", "Calling Bird", "Gold Ring", "Geese-a-Laying", "Swan-a-Swimming", "Maid-a-Milking", "Lady Dancing", "Lord-a-Leaping", "Piper piping", "Drummer Drumming");              
?>
                                

In the example above, the partridge is in room zero, the turtle dove is in room 2, etc.

Look at the drop-down menu of 12 gifts in the form. It was created with a function with only ten lines! To add additional options, we'd only have to add more elements to our array!

The Create Select Menu from Array Function:

Here's the nifty function that creates our drop-down menu:

                                
function createSelectMenuFromArray($name, $arr, $selectedValue){
    echo "<select name=\"$name\" id=\"$name\">";
    $length = count($arr); //number of elements in $arr
    for($ndx = 0; $ndx < $length; $ndx++){
        $element = $arr[$ndx];
        if($ndx == ($selectedValue  - 1)) echo "<option value=\"$ndx\" selected>$element</option>";
        else echo "<option value=\"$ndx\">$element</option>";
    }
    echo "</select>";
}//end function createSelectMenuFromArray
                                

Notice that in PHP, when we create an attribute for a tag like 'name="menu"' we must 'escape' the quotes within PHP using the backslash (\). Why? Because a quote is a programatic character in PHP (it has code-meaning) and the \" routine is a way to make it act like a normal character.

See how we count elements in an array and use that count to construct a for loop that iterates through all elements and places them in 'option's that are echoed into the webpage? That's what is so powerful about PHP: we can automate certain processes with functions, place those functions in utility libraries, then use them whenever we need them.

To pre-select a 'default' value (in this case, it was 'Gold Ring' with array index number 4 (the 5th element, by the way)) we used if-else selection logic to see if control variable $ndx was equal (==, by the way) to $selectedValue. If it was, we included the 'selected' attribute in the option tag. Otherwise that attribute was not included.

In this way, we could customize how the drop-down menu work when first loaded.

12/17/19


What would you like to send Marco?