You're here: Snippet Directory » PHP (128)
Language:

build_select_box_from_array ()

Language: English
Programming Language: PHP
Published by: bigdisk [not registered]
Last Update: 5/15/2006
Views: 1145


Description

Takes PHP arrays and builds a pop-up HTML box from it. You can optionally pass in a value to tell it with item is SELECTED by default.Originally published on http://www.phpbuilder.com/

Code

1 function build_select_box_from_array ($vals, $select_name, 2 $checked_val = "NO WAY YOU CAN HAVE THIS VALUE!!!") { 3 /* 4 Takes one array, with the first array being the "id" or value 5 and the array being the text you want displayed 6 7 The second parameter is the name you want assigned to this form element 8 9 The third parameter is optional. Pass the value of the item that should be checked 10 */ 11 12 // clear buffer 13 $buffer = ""; 14 15 // check for rows, if there is nothing, return 0 16 $rows = ( !empty($vals) ? count($vals) : 0); 17 18 // kick the bucket if we don't have any values 19 if ($rows < 1) return false; 20 21 // header for select box 22 $buffer .= "<SELECT NAME=\"".htmlentities($select_name)."\">\n"; 23 24 // loop through values 25 for ($i=0; $i<$rows; $i++) { 26 $buffer .= " <OPTION VALUE=\"".htmlentities($i)."\"". 27 ($i==$checked_val ? "SELECTED>" : ">"). 28 htmlentities($vals[$i])."</OPTION>\n"; 29 } // for each row 30 31 // end of select box 32 $buffer .= "</SELECT>\n"; 33 34 // return the buffer 35 return $buffer; 36 37 } // end function build_select_box_from_array

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS