PHP/HTML: Print <select><options></select>

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

PHP/HTML: Print <select><options></select>

Post by scorphus »

Code: Select all

<?php
/**
 * @author Scorphus
 * @return string
 * @param $optArray Array
 * @param $selName string
 * @param $selected string
 * @param $addAttrib string
 * @desc Return a string containing the HTML code of a select/options tags
 * according to $optArray's keys and values.
 */
function printSelect ($optArray, $selName = '', $selected = '', $addAttrib = '') {
	$conteudo = '';
	if (!empty($selName))
		$conteudo .= "<select name="$selName" $addAttrib>\n";
	foreach ($optArray as $key => $value)
		$conteudo .= "\t<option label="$key" value="$key"" . ($key == $selected ? ' selected' : '') . ">&nbsp;$value&nbsp;</option>\n";
	if (!empty($selName))
		$conteudo .= "</select>\n";
	return $conteudo;
}
?>
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

This is one of the many functions of my very large funcbase.php file. I decided to share some of them with the community. This is the first.

Examples of usage:

Code: Select all

<?php
$opt = array(
	'opt1' => 'This is the option #1',
	'opt2' => 'This is the option #2',
	'opt3' => 'This is the option #3',
	'opt4' => 'This is the option #4',
	'opt5' => 'This is the option #5'
);

echo "Just print the options tags in case the select tag is already present:\n";
echo printSelect($opt), "\n";

echo "Print the select tag and the options tag inside it:\n";
echo printSelect($opt, 'options'), "\n";

echo "Now we say which option is selected by default:\n";
next($opt);
echo printSelect($opt, 'options', key($opt)), "\n";

echo "Now take the form information to tell which option is default, and also an additional attribute (onChange for a jump menu):\n";
next($opt);
$_POST['options'] = key($opt);
echo printSelect($opt, 'options', $_POST['options'], 'onChange="javascript:document.form1.submit();"'), "\n";
?>
output:

Code: Select all

Just print the options tags in case the select tag is already present:
	&lt;option label="opt1" value="opt1"&gt;&amp;nbsp;This is the option #1&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt2" value="opt2"&gt;&amp;nbsp;This is the option #2&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt3" value="opt3"&gt;&amp;nbsp;This is the option #3&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt4" value="opt4"&gt;&amp;nbsp;This is the option #4&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt5" value="opt5"&gt;&amp;nbsp;This is the option #5&amp;nbsp;&lt;/option&gt;

Print the select tag and the options tag inside it:
&lt;select name="options" &gt;
	&lt;option label="opt1" value="opt1"&gt;&amp;nbsp;This is the option #1&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt2" value="opt2"&gt;&amp;nbsp;This is the option #2&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt3" value="opt3"&gt;&amp;nbsp;This is the option #3&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt4" value="opt4"&gt;&amp;nbsp;This is the option #4&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt5" value="opt5"&gt;&amp;nbsp;This is the option #5&amp;nbsp;&lt;/option&gt;
&lt;/select&gt;

Now we say which option is selected by default:
&lt;select name="options" &gt;
	&lt;option label="opt1" value="opt1" selected&gt;&amp;nbsp;This is the option #1&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt2" value="opt2"&gt;&amp;nbsp;This is the option #2&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt3" value="opt3"&gt;&amp;nbsp;This is the option #3&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt4" value="opt4"&gt;&amp;nbsp;This is the option #4&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt5" value="opt5"&gt;&amp;nbsp;This is the option #5&amp;nbsp;&lt;/option&gt;
&lt;/select&gt;

Now take the form information to tell which option is default, and also an additional attribute (onChange for a jump menu):
&lt;select name="options" onChange="javascript:document.form1.submit();"&gt;
	&lt;option label="opt1" value="opt1"&gt;&amp;nbsp;This is the option #1&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt2" value="opt2" selected&gt;&amp;nbsp;This is the option #2&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt3" value="opt3"&gt;&amp;nbsp;This is the option #3&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt4" value="opt4"&gt;&amp;nbsp;This is the option #4&amp;nbsp;&lt;/option&gt;
	&lt;option label="opt5" value="opt5"&gt;&amp;nbsp;This is the option #5&amp;nbsp;&lt;/option&gt;
&lt;/select&gt;
Hope it's gonna be useful for you.

Regards,
Scorphus.
Post Reply