Simple function for drawing HTML dropdown list from various sources
If you’re still lurking in the darkness of writing code with pure PHP and not using any framework then in this post you can find some nifty function, useful for drawing a HTML list boxes.
Basically it tries to render (draw) a <select>
element basing on input data provided in $source
variable. It can be either a list (key=>value) or string, where values and items’ texts are separated with delimiter. Such string can be provided directly or read from file. In this case you only provide path to a file (source of data).
Here’s function declaration:
[code language=”php”]
/**
* Draws XHTML <select></select> element based on source data. It can be either an array
* (key->value pairs) or string or path and filename of a file containing string.
* In both cases, each <select></select> elements (<option>) should be put into new line
* and each line should contain id of element separated from value with a delimiter.
*
* @param array $source Data used as select’s items (array, string or path).
* @param string $selected Selected value (if any).
* @param string $options Options for drawn select element.
*
* @return string XHTML code for generated select element.
*/
function drawSelect($source, $selected = ”, $options = array())
{
$combo = ”;
$data = array();
/*
* Default options
*/
$id = isset($options[‘id’]) ? $options[‘id’] : ”;
$size = (isset($options[‘size’])) ? $options[‘size’] : 1;
$class = isset($options[‘class’]) ? $options[‘class’] : ”;
$style = isset($options[‘style’]) ? $options[‘style’] : ”;
$break = isset($options[‘lineBreak’]) ? $options[‘lineBreak’] : "n";
$delimiter = isset($options[‘delimiter’]) ? $options[‘delimiter’] : ‘#’;
$disabled = (isset($options[‘disabled’]) && $options[‘disabled’] == true) ? ‘ disabled="disabled"’ : ”;
if(!is_array($source))
{
if(is_file($source)) $source = file_get_contents($source);
$source = explode($break, $source);
foreach($source as $element)
{
list($value, $text) = explode($delimiter, $element);
$data[$value] = $text;
}
}
else $data = $source;
$combo = ‘<select style="’.$style.’" class="’.$class.’" id="’.$id.’" size="’.$size.’"’.$disabled.’>’;
foreach($data as $value=>$text)
{
$combo .= ‘<option value="’.$value.’"’.(($value == $selected) ? ‘ selected="selected"’ : ”).’>’.trim($text).'</option>’;
$combo .= "n";
}
$combo .= ‘</select>’;
return $combo;
}
[/code]
Usually and by default, each <option></option>
element is put into separate line in source data (i.e. separated by a line break) and value from text is separated by hash (#
). But you can change it any way you’d like with $options
variable.
Input parameters ($options
elements) are pretty self-explanatory. If you have any problems understanding, how to use this variable or whole function, take a look at these examples of calling it:
[code language=”php”]
$options = array
(
‘disabled’=>true,
‘forceList’=>true,
‘delimiter’=>’@’,
‘lineBreak’=>"br",
‘id’=>’tweaked-up-select’
);
$testFile = ‘test.data’;
$testArray = array(1=>’One’, ‘two’=>’Two’, 3=>’Something’);
$testString = ‘1@One’."br".’two@Two’."br".’3@Something’;
echo(drawSelect($testFile, 1));
echo(drawSelect($testArray, 3));
echo(drawSelect($testString, ‘two’, $options));
[/code]
I’m using this function along with my {simple class for rendering pages](http://onezeronull.com/2012/11/16/simple-page-rendering-class/). However, only in non-Yii applications, which I try to limit to the minimum. Because… coding web applications or web pages with Yii is a dream!