<?php
/**
* 该函数自动生成 select 下拉框,并记录上次选定项
* Versions 1.0 written by Bigdisk
* This version is 3.0 , written by Macro Zeng
* You can submit a new version of this snippet to http://www.ctohome.com
* if you have modified it and you feel it is appropriate to share with others.
**/
$option = array("Welcome"=>"1", "to"=>"2", "ctohome"=>"3", "dot"=>"4", "com"=>"5");
$select_name = "www_ctohome_com";
$checked_val = ($$select_name) ? $$select_name : "3";
$selector = build_select_box_from_array ($option, $select_name,$checked_val);
function build_select_box_from_array ($option, $select_name,
$checked_val = "NO WAY YOU CAN HAVE THIS VALUE!!!") {
/*
The first parameter is an array to keep the option's name and value
The second parameter is the name you want assigned to this form element
The third parameter is optional. Pass the value of the item that should be checked
*/
// separate the option array to vars and values array
while (list($vars[], $values[]) = each ($option));
// clear buffer
$buffer = "";
// check for rows, if there is nothing, return 0
$rows = ( !empty($vars) ? count($vars) : 0);
// kick the bucket if we don't have any values
if ($rows < 1) return false;
// header for select box
$buffer .= "<SELECT NAME="".htmlentities($select_name)."">n";
// loop through values
for ($i=0; $i < ($rows - 1); $i++) {
$buffer .= " <OPTION VALUE="".htmlentities($values[$i]).""".
($values[$i]==$checked_val ? " SELECTED>" : ">").
htmlentities($vars[$i])."</OPTION>n";
} // for each row
// end of select box
$buffer .= "</SELECT>n";
// return the buffer
return $buffer;
} // end function build_select_box_from_array
?>
<style type="text/css">
<!--
body {font-size:9pt}
-->
</style>
<FORM METHOD=POST ACTION="">
<?= $selector ; ?>
<INPUT TYPE="submit">
</FORM>
|