State Your Case: Associative Arrays in PHP

The greatest state?
State of Texas
State Selection Form

In TNT, we have an app that lets you select a state and click on links that take you to governmental sites that tell you information about that state. An 'associative array' in PHP was used to store 'key-value' pairs of state names (keys) with their abbreviations (values). Here's the array that was in a P-COW as the page loaded:

$stateInfo = array(
    'Alabama'=>'AL',
    'Alaska'=>'AK',
    'Arizona'=>'AZ',
    'Arkansas'=>'AR',
    'California'=>'CA',
    'Colorado'=>'CO',
    'Connecticut'=>'CT',
    'Delaware'=>'DE',
    'Florida'=>'FL',
    'Georgia'=>'GA',
    'Hawaii'=>'HI',
    'Idaho'=>'ID',
    'Illinois'=>'IL',
    'Indiana'=>'IN',
    'Iowa'=>'IA',
    'Kansas'=>'KS',
    'Kentucky'=>'KY',
    'Louisiana'=>'LA',
    'Maine'=>'ME',
    'Maryland'=>'MD',
    'Massachusetts'=>'MA',
    'Michigan'=>'MI',
    'Minnesota'=>'MN',
    'Mississippi'=>'MS',
    'Missouri'=>'MO',
    'Montana'=>'MT',
    'Nebraska'=>'NE',
    'Nevada'=>'NV',
    'New Hampshire'=>'NH',
    'New Jersey'=>'NJ',
    'New Mexico'=>'NM',
    'New York'=>'NY',
    'North Carolina'=>'NC',
    'North Dakota'=>'ND',
    'Ohio'=>'OH',
    'Oklahoma'=>'OK',
    'Oregon'=>'OR',
    'Pennsylvania'=>'PA',
    'Rhode Island'=>'RI',
    'South Carolina'=>'SC',
    'South Dakota'=>'SD',
    'Tennessee'=>'TN',
    'Texas'=>'TX',
    'Utah'=>'UT',
    'Vermont'=>'VT',
    'Virginia'=>'VA',
    'Washington'=>'WA',
    'West Virginia'=>'WV',
    'Wisconsin'=>'WI',
    'Wyoming'=>'WY'
);

This array was used to build a 'drop down' menu dynamically in PHP. Here's the code for the form:

<form name="selectState" method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
    <fieldset>
    <legend>Choose a state:</legend>
    <p>
        <label for="stateAbbrev">State:</label>
        <select name="stateAbbrev" id="stateAbbrev">
            <?php
                //assumes the 'stateInfo' associative array is in the mind of the main page
                $maxNum = sizeof($stateInfo);
                $theStates = array_keys($stateInfo);
                $stateAbbreviations = array_values($stateInfo);
                for($i = 0; $i<$maxNum; $i++){
                    $myState = $theStates[$i];
                    if($myState == $stateName)
                        echo "<option value=\"$stateAbbreviations[$i]\"
selected=\"selected\">$myState</option>";
                    else
                        echo "<option value=\"$stateAbbreviations[$i]\">$myState</option>";
                }
            ?>
        </select>        
    </p>
    <p>
      <input type="submit" name="submit" id="submit" value="Submit">
    </p>
    </fieldset>
</form>

Comments

admin's picture

I may be state-ing the obvious, but this code is important because it shows how to build a menu dynamically if you have data in an array. As 2nd-year web design students begin putting information in a mySQL data base, they may find that they need to make menus out of that data. Hmmm...

  1. Store information in a mySQL database
  2. Establish a link (connection) to the database
  3. Formulate a query to get the information you want as a data set
  4. Read the data into a PHP array
  5. Create the select menu using that array information as shown in this post!

 

admin's picture

If you run the original app, you will notice that the select menu 'pre-populates' with 'Texas.' How? In the P-COW before the page loads (above the doctype) we have the following code snippet:

if(isset($_POST['submit'])){
    $stateAbbrev = $_POST['stateAbbrev'];
    $stateName= array_search($stateAbbrev, $stateInfo);
}else{
    $stateName = "Texas";
}

Notice that before the page is submitted, the variable '$stateName' is assigned to Texas. When the loop generates the menu items, the selection logic causes the attribute 'selected' to be added for the entry Texas

admin's picture

In the snippets above, notice how I aligned the array entries. Although it meant a long verticle chunk of code, it made viewing the information easier from an editting point of view. Also, notice the form code: nested items are indented, making it easier to find problems if they occur