_
_ Handy PHP
ezyrewards - free domains, ipods, vouchers and more
_
_
Tutorials, Scripts, Information And Other Resources arrow PHP Functions arrow Build Select Field
Friday, 25 July 2008
_

Refer A Friend using Revolution Money Exchange
Resource Menu
Handy PHP Store
Free Domain Name
PHP Functions
PHP Downloads
PHP Newsfeeds
PHP Tutorials
Webmaster Tools
User Login
TalkPHP
Get friendly help with your PHP problems for free.
www.talkphp.com
The PHP Resource Index
Handy PHP
Free PHP Help!


 
This Section Is Scheduled
For Maintenance

    The format of the PHP Functions section here at Handy PHP needs some attention soon.  Recent changes to the website have brought to light some significant layout problems with this section.  The need for a standard format for all functions has resulted in the problems.  Each function will be reformated, enhanced, archived, and updated as time allows. In the meantime, some screen resolutions may display a distorted version of the website.
Thank you for your visit and patience.

If you encounter any major problems with viewing the functions, please inform us via the forums.
If you are unable to find what you needed here, please let us know what you wanted in the forums so that we may add relevant content in the future.

Build Select Field Print E-mail
User Rating: / 2
PoorBest 
PHP Resources - Handy PHP Functions
Written by M. L. Griswold   
Saturday, 21 October 2006
Description:
  string build_select_field(string name, array selections, string selected[, string options[, string prepemd[, string prepend[, string append[, string indent]]]]])
     
  This handy PHP function will reduce the amount of typing you do when you are designing HTML forms.  While all you need to enter is the field name and an array of select options for the function to work, it offers the flexability to build very complex select tags if needed.  The selected parameter allows for a default option to be selected.  The "option" parameter allows for extra arguments to be inserted inside of the input tag while the "prepend" and "append" parameters allow for additional output to be added before and after the tag.   The indent tag allows for indenting the output HTML for easier reading.
     
Function:
  function build_select_field($name, $select_array, $selected = '', $option = '', $prepend = '', $append = '', $indent = '') {
// Function written by Marcus L. Griswold (vujsa)
// Can be found at http://www.handyphp.com
// Do not remove this header!
    $field = $indent . $prepend . "<select name=\"$name\"";
    if($option){
        $field .= " $option";
    }
    $field .= ">\n";
    for ($x = 0; $x < count($select_array); $x++) {
        if ($select_array[$x] == $selected) {
            $field .= $indent . "\t<option selected>$select_array[$x]</option>\n";
        }
        else {
            $field .= $indent . "\t<option>$select_array[$x]</option>\n";
        }
    }
    $field .= $indent . "</select>" . $append . "\n";
    return $field;
}
   
 
Usage:
  echo build_select_field('sample', array('Option 1', 'Option 2', 'Option 3'), 'Option 2', 'multiple', '<b>Options: </b>', '<br />', "\t\t\t");
     
Result:
 
            <b>Options: </b><select name="sample">
                <option>Option 1</option>
                <option selected>Option 2</option>
                <option>Option 3</option>
            </select><br />
     
Notes:
  This is only really practical if you are planning to add a lot of select fields manually.  Using a loop to build a form would be a good substitute for this kind of manual form creation.


Discuss this article on the forums. (1 posts)
 
< Prev   Next >

_
 
_
_
© 2008 Handy PHP