ajlisowski Posted June 27, 2011 Share Posted June 27, 2011 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? Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted June 28, 2011 Share Posted June 28, 2011 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.. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 28, 2011 Share Posted June 28, 2011 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 Quote Link to comment Share on other sites More sharing options...
ajlisowski Posted June 28, 2011 Author Share Posted June 28, 2011 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.