PHP State Drop Down Menu - Reusable Code!


Intermediary Functions for Multi Dimentional Arrays in Drop Down Menus

Using the multi-dimensional arrays requires an intermediary function (or code snippet, that's up to your needs, I use functions so I can continue to re-use it easily.

So if I want a drop down that looks like <option value="jul">July (7)</option> [...]

I'd manipulate the array to reflect array('jul' => 'July (7)' [...]);

Here's the function that will do that for us, and then pass that new 'temporary' array to the showOptionsDrop function we earlier perfected. We'll only be passing the active value this time and forcing the function to return and calling the array as global within the monthOptionsDrop function.

PHP

<?php
    // Note this is the same function as the previous page
    function showOptionsDrop($array, $active, $echo=true){
        $string = '';

        foreach($array as $k => $v){
            $s = ($active == $k)? ' selected="selected"' : '';
            $string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";     
        }

        if($echo)   echo $string;
        else        return $string;
    }

    // Here's our new intermediary function
    function monthOptionsDrop($active){
        global $month_arr;

        foreach($month_arr as $k => $v){
            $short = substr( strtolower($v[1]),0,3 );       //lowercase, and pull the first 3 letters NOT the abbreviation
            $month_arr_new[$short] = $v[1] . " ($k)";       
        }

        return showOptionsDrop($month_arr_new, $active, false);
    }
?>

Which works like a champ like this (which will highlight <option value="mar" selected="selected">March (3)</option>):

HTML/PHP

<select name="months">
    <option value="0">Choose a month</option>
    <?php echo monthOptionsDrop(3); ?>
</select>

Hope that helped you think about working with PHP arrays and drop down menu's differently. It may not be the best solution but it always works for me (quick and easy too!)


Update: March 14, 2008

Read more about modifying this code to make it work for drop down menu's that have and can select multiple options.

Multiple Select Drop Down Menu in PHP are an easy modification to this lesson and it's also backwards compatible in case you need to improve the code for your needs at a later time.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


55 subscribers couldn't be wrong*!
Subscribe to the Ninedays Blog feed!
* Not statistically proven, they could be wrong.
Similar Entries
Encoding & and HTML Entities in PHP
Special characters like MS Word's smart quotes can muck up a good custom piece to CMS work. First we're going to go over using a regular expression to make sure that & and any character code starting with & can always be save, and displayed in a form or webpage.
PHP Serialize, What’s it Do? What’s it For?
An explanation of how the PHP serialize() and unserialize() functions can be helpful for you as far as storing a PHP array in a MySQL database and still begin able to attain it's PHP variable qualities quickly upon retrieval.
Multiple Select Drop Down Menu in PHP
It's very easy to take what we did back in January for reusable PHP drop down menu code and make it work for multi value select elements.
Special Characters in Custom CMSs and Forms
Encoding special characters can be tricky, especially when you have clients with custom CMSs who like to copy and paste all the em dashes and trademark symbols from word into your web based text area. Now I have found a solution for my needs!
Path and Directory Function in PHP
Some great handy PHP functions for dissecting file paths and getting full paths for how they relate to the file you are in. Never again shall we explode or split a string on / or .!
Next Post
Limit Characters in a Textarea with Prototype
Previous Post
Happy New Year

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Trackbacks & Pingbacks

Reader Comments

Nice code. Your formatting could use some work though.

Ya interesting ;)

Thanks for the arrays.

m