Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Forked from gavinblair/selectgender1.html
Created June 7, 2010 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanJA/429268 to your computer and use it in GitHub Desktop.
Save SeanJA/429268 to your computer and use it in GitHub Desktop.
<?php
/**
* Create a select box
* @param string $name the name of the select box
* @param array $options the options, associative array array('val_1'=>'opt_1' ... )
* @param string $selected the option that is selected
* @param array $attributes associative array of attributes for the select box array('attr_1'=>'val_1', 'attr_2'=>'val_2')
*/
function html_select($name, array $options, $selected=null, array $attributes = array()) {
$select = '<select name=".'.$name.'" ';
foreach ($attributes as $a=>$v) {
$select .= $a .'="' . $v . '"';
}
$select .= ' >';
foreach($options as $oid => $option) {
$select .= '<option ';
if($selected == $oid) {
echo ' selected="selected" ';
}
$select .= ' value="'.$oid.'">.'.$option.'</option>';
}
$select .= '</select>';
echo $select;
}
$options = array(
1 => "Male",
2 => "Female"
);
$attributes = array(
'class'=>'simple_selectbox',
'disabled'=>'disabled',
);
html_select('gender', $options, '1', $attributes);
@SeanJA
Copy link
Author

SeanJA commented Jun 8, 2010

If you don't do the "array" part I can pass in anything I want, it is just an extra check that php will automagically do for me instead of me having to make sure it is by calling is_array().

It works even better if you have something like:

function (myObject $object){ ... }

If you pass anything other than myObject in it will not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment