siteIcon

Tech Novice Tools

PHP Apps

burgerIcon

Marco Poloexplorer

Stage 3a:
Error Trapping User Dabbling
with GET variables identifiers

About the app

Did you read the preamble to this stage in the Marco Polo Saga Index?

If you didn't you won't really appreciate this stage.

Run the app as normal and then change the 'giftNdx' variable name to something else, like 'gift'.

This time, the app should address the error. To compare and contrast with previous code, look at Marco Polo - Stage 3.

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.

01/07/20


What would you like to send Marco?