Jump to content

Storing dropdown options...


ajlisowski

Recommended Posts

Hey all. I was just wondering what you guys think is the best method for storing dropdown options. Ive almost always used mysql and had a table for each list to allow myself to pull all the correct info with one query.

 

However it seems a bit overboard to use mysql to store a list of 3-4 static options.

 

Does it make sense to set these as global arrays at the start of my application?

 

Ive been experimenting with storing them in an xml. This works fine, but I wonder if it is better then the other options? Seems like mysql or a global array would be best for performance, but maybe im wrong. Whats the best way? I would assume it would matter how big the lists are? Would a list of States make more sense in mysql where as choice of gender Male/Female/Not sharing make sense in either xml or an array?

Link to comment
Share on other sites

If this 3-4 option list is just a static list that maybe the options change once in a blue moon, but you have the same options on a dozen pages.. you could always

 

in a separate file put the static HTML version of this select box. There where ever you want that particular group of options you could include() the file in via the php. This way you in concept have a dynamic box that can be used anywhere but all the while retain the ability to be static without having to go into multiple pages to update/edit it. This way you also don't have to come up with extra code to read the XML file you would pull it from otherwise.. but this is just a thought..

Link to comment
Share on other sites

what I normally do in these cases is store arrays in a separate file. I don't store the entire dropdown html for the simple reason that sometimes I want it to have different names, and sometimes I want it to be static, others I want it to be auto-selected with a value, so I do something like this:

 

array.txt

$combo = array(
"newton"=>"Isaac Newton",
"einstein"=>"Albert Einstein",
"davinci"=>"Leonardo DaVinci"
);

 

and just use:

 

include_once('array.txt');
echo '<select name="whatever" size="1">';
foreach($combo as $value => $name){
    echo '<option value="'.$value.'">'.$name.'</option>';
}
echo '</select>';

 

this way I can also do this:

 

include_once('array.txt');
echo '<select name="whatever" size="1">';
foreach($combo as $value => $name){
    echo '<option value="'.$value.'"';
    if($value == $defaultValue) echo ' selected';
    echo '>'.$name.'</option>';
}
echo '</select>';

 

and also have the option do add a blank value when needed:

 

include_once('array.txt');
echo '<select name="whatever" size="1">';
echo '<option value=""></option>';
foreach($combo as $value => $name){
    echo '<option value="'.$value.'"';
    if($value == $defaultValue) echo ' selected';
    echo '>'.$name.'</option>';
}
echo '</select>';

 

 

Its just my personal preference for small dropdowns that hardly ever need updating,

 

hope this helps

Link to comment
Share on other sites

Storing the html wouldnt work too well because i use the values outside of a dropdown. I also use them as part of a presentation piece for displaying profile info.

 

I like the idea of just having an array.txt file that i can include which would set those smaller arrays for me. I went with the xml route because i am using zend framework and i already have config data, navigation data, ACL data in xml and figure having an xml for all my select arrays would be consistant with the rest of my framework.

 

But either way, the consensus is to not store these smaller ones in a database, correct?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.