Muffinresearch Labs by Stuart Colville

Using PHP to generate select boxes | 3 Comments

Posted in Code on 12th December 2004, 7:10 pm by

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.

Post Tools

  • Karl

    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/

  • Julie

    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.

  • http://muffinresearch.co.uk Stuart Colville

    @Julie – I’ve updated the script to quote the keys

GNU screen: open tab in current working directory|(1)

A nice trick for having screen open a new tab in the same directory as the one you’re currently in. To use it add it to your .screenrc

# Open new window in current dir.
bind c stuff "screen -X chdir \$PWD;screen^M"
bind ^c stuff "screen -X chdir \$PWD;screen^M"

Hat tip: mteckert on SuperUser.com

Ubuntu: add-apt-repository: command not found|(2)

When you’re using a minimal Ubuntu install if you find the ‘add-apt-repository’ command is missing (it’s useful for adding PPAs and other repositories), then simply run:

sudo apt-get install python-software-properties

Photos on Flickr

© Copyright 2004-12 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.517s.