Today is the last day I will have to dig around for a drop down of states to use on a web site. I’ve finally stopped being lazy and added a Smarty function to PICKLES to allow for easy insertion of a state (50 + DC) select box. Later enhancements may come to support Puerto Rico and the Canadian provinces, but for right now, it’s just the United States (as that’s all I needed on there). PHP with PICKLES (soon to be released maybe?) automatically loads this function in for me, but if you’re interested in using it and you’re not using PICKLES, you may want to consult this page for how to register a custom Smarty function.

<?php

/**
 * Smarty State Drop Down for PICKLES
 *
 * PICKLES is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * PICKLES is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with PICKLES.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * @author    Joshua John Sherman <[email protected]>
 * @copyright Copyright 2009 Joshua John Sherman
 * @link      http://phpwithpickles.org
 * @license   http://www.gnu.org/copyleft/lesser.html
 * @package   PICKLES
 */

/**
 * Smarty function: html_select_state
 *
 * Allows you to add a state drop down easily in Smarty (in the vein of the
 * built-in functions html_select_time and html_select_date).  Includes the
 * 50 states and the District of Columbia.  Eventually there may be an
 * enhanced version that includes Puerto Rico and the Canadian provinces.
 *
 * @param array  Parameters array
 * @param object Smarty object
 * @usage <code>{html_select_state prefix="user_"}</code>
 */
function smarty_function_html_select_state($params, &$smarty) {

    // Sets up the prefix and title (optionally passed in)
    $prefix = (isset($params['prefix']) ? $params['prefix'] : null) . 'state';
    $title  = (isset($params['title'])  ? $params['title']  : null);

    // Puts together the dropdown
    $select = '
        <select id="' . $prefix . '" name="' . $prefix . '" title="' . $title . '">
            <option value="">-- Select a State --</option>
            <option value="AK">Alaska</option>
            <option value="AL">Alabama</option>
            <option value="AR">Arkansas</option>
            <option value="AZ">Arizona</option>
            <option value="CA">California</option>
            <option value="CO">Colorado</option>
            <option value="CT">Connecticut</option>
            <option value="DC">District of Columbia</option>
            <option value="DE">Delaware</option>
            <option value="FL">Florida</option>
            <option value="GA">Georgia</option>
            <option value="HI">Hawaii</option>
            <option value="IA">Iowa</option>
            <option value="ID">Idaho</option>
            <option value="IL">Illinois</option>
            <option value="IN">Indiana</option>
            <option value="KS">Kansas</option>
            <option value="KY">Kentucky</option>
            <option value="LA">Louisiana</option>
            <option value="MA">Massachusetts</option>
            <option value="MD">Maryland</option>
            <option value="ME">Maine</option>
            <option value="MI">Michigan</option>
            <option value="MN">Minnesota</option>
            <option value="MO">Missouri</option>
            <option value="MS">Mississippi</option>
            <option value="MT">Montana</option>
            <option value="NC">North Carolina</option>
            <option value="ND">North Dakota</option>
            <option value="NE">Nebraska</option>
            <option value="NH">New Hampshire</option>
            <option value="NJ">New Jersey</option>
            <option value="NM">New Mexico</option>
            <option value="NV">Nevada</option>
            <option value="NY">New York</option>
            <option value="OH">Ohio</option>
            <option value="OK">Oklahoma</option>
            <option value="OR">Oregon</option>
            <option value="PA">Pennsylvania</option>
            <option value="RI">Rhode Island</option>
            <option value="SC">South Carolina</option>
            <option value="SD">South Dakota</option>
            <option value="TN">Tennessee</option>
            <option value="TX">Texas</option>
            <option value="UT">Utah</option>
            <option value="VA">Virginia</option>
            <option value="VT">Vermont</option>
            <option value="WA">Washington</option>
            <option value="WI">Wisconsin</option>
            <option value="WV">West Virginia</option>
            <option value="WY">Wyoming</option>
        </select>
    ';

    // Returns the dropdown to Smarty
    return $select;
}

?>




Did you enjoy this post?

Cool if I slip into your inbox with more?
Full posts, 1-2 times per week: