开发者

creating a state drop down list

开发者 https://www.devze.com 2023-04-04 02:29 出处:网络
I know how to create a drop down list, however if you had to create a form w开发者_运维知识库here one of the input is a state (a two letter word) in a drop down list, do I just input the state one by

I know how to create a drop down list, however if you had to create a form w开发者_运维知识库here one of the input is a state (a two letter word) in a drop down list, do I just input the state one by one or should I store the state in a DB?


A list of states really aren't updated that often, and it seems like overkill to use a database to store them. I don't know what your backend technology is, but I would likely create a State dropdown control (or whatever passed for a control in your environment) which can be used in multiple locations, so in the rare instance you have to modify your state list, it's only done in a single location. You could also store the states as a list in a configuration system of some kind, but I can't give more details without knowing more about your stack.


This is kinda weird question. Depending on your project of course.

If you need performance and speed, when loading your dropdown list (I'm assuming you meant <select />). Then you might want to put them into a separate .php file in an array form. So later on you could just loop them with foreach(). This method will surely load faster, then to load 50 states from DB.

inc-states.php:

$states = array(
    1 => 'Alabama',
    2 => 'Indiana',
    3 => 'Nevada'
    // etc..
);

Generate form:

echo '<select name="states">';
foreach ($states as $key => $state) {
    echo '<option value="' . $key . '">' . $state . '</option>';
}
echo '</select>';

So after posting you will only save the keys. Later on you can again get the right state via the key:

include('inc-states.php')

$user['state'] = 2; // Lets make a default for this example

print $states[$user['state']]; // outputs Indiana

Same basic idea remains with the DB method as well. Only this is probably faster.. Unless of course you want to be able to edit the states via some sort of admin console.

NOTE: You in fact said two character shortcodes only, but my example array looks prettier with full titles. Anyways, as you probably know, you can put the states in whatever format in the array.


I would put the list in a db, that way if you want to store id's for references instead of saving the state text, it would be easier to manage a relational db that way.

0

精彩评论

暂无评论...
验证码 换一张
取 消