/*
Here's a very handy class function for printing HTML SELECT. I normally use it with dynamic
arrays i.e. result set from an SQL query
*/
class html {
function print_select($select_name, $select_array, $selected_value="", $js="", $multiple="")
{
//
// print a select box
// parameters:
// 1. name of the select box
// 2. array with keys and values
// 3. selected value(s)
// 4. javascript
// 5. multiple
//
print ( "\n<SELECT NAME=\"" . $select_name . "\" " .
$multiple . " " . $js . ">\n" );
while (list ($key, $val) = each ($select_array)) {
if ($multiple == "MULTIPLE") {
if (is_array($selected_value)) {
if (in_array($key, $selected_value)) {
print ( "\t<OPTION VALUE=\"" .
$key . "\" SELECTED>" . $val ."</OPTION>\n" );
} else {
print ( "\t<OPTION VALUE=\"" .
$key . "\">" . $val ."</OPTION>\n" );
}
} else {
print ( "\t<OPTION VALUE=\"" .
$key . "\">" . $val ."</OPTION>\n" );
}
} else {
if ($key == $selected_value) {
print ( "\t<OPTION VALUE=\"" .
$key . "\" SELECTED>" . $val ."</OPTION>\n" );
} else {
print ( "\t<OPTION VALUE=\"" .
$key . "\">" . $val ."</OPTION>\n" );
}
}
}
print ( "</SELECT>\n" );
}
//
// Put some other functions here ...
//
}
/*
Usage example:
*/
// This is the file with the class
include_once("html.inc.php");
// Initiate the class
$html = new html;
// Get the result set from SQL query
// Some database related code here ... put the results into $result_array
// Print the select box
$html->print_select("select_name", $result_array, $selected_element,
"OnChange=\"this.form.submit();\"");
|