Using PHP to generate select boxes | Comments (3)
Posted in Code on 12th December 2004, 7:10 pm by Stuart
I wrote a little tool to assist with generating select boxes from arrays in PHP. If you have got your form already drawn out in PHP and then you decide you need to use PHP to get the selected item from a stored value in a session or database this can be a real pain. Usually you have to create an array of all of your options now this is fine if you are talking about a few items but if it’s a lot of options this tool creates the array for you. Simply paste in theoptions along with their tags. Comes complete with an example function for creating select boxes from arrays too. See Select Box array generator for more info.

Thanks for the code to convert an array to a select list, but could you post the function you use to convert the selectbox to the value-option pairs MD array (Selectbox Converter) used via the form? THAT is what I really need.
Thanks!
http://muffinresearch.co.uk/code/php/select2array/
Great script!! I too would love to see the code that builds the value-option multi-dimensional array so I can tweak it. The problem with the existing array output is there needs to be quotes around the array keys:
currently generates:
$multi = array(AF => “Afghanistan”, AL => “Albania”, DZ => “Algeria”, AD => “Andorra”);
correct PHP array syntax should be:
$multi = array(“AF” => “Afghanistan”, “AL” => “Albania”, “DZ” => “Algeria”, “AD” => “Andorra”);
For the most part it would still work but in my case trying to use an array of countries causes a syntax error on country codes “AS” and “DO” because PHP doesn’t see them as strings and tries to interpret them as commands.
@Julie – I’ve updated the script to quote the keys