Re: html form question

From: Date: Thu, 26 Jul 2001 01:01:45 +0000
Subject: Re: html form question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-59650@lists.php.net to get a copy of this message
On Thu, 26 Jul 2001 10:27, CGI GUY wrote: > What's the method for populating any number of html > form <option>...</option> tags with query results? > I've seen lots of php-embedded examples for CHECKBOX, > RADIO and even SELECT, but I can't seem to figure out > how to create a simple drop-down menu. HELP!!! You only need to loop through the query result in the usual fashion and echo the results within option tags. Here's a function I wrote to to useful things with SELECT: <?php /* formDropDown - create an HTML <SELECT> * vars: $name - the form variable NAME * $value - the SELECTED option which may be an array for multiple selected items * $labels - assoc. array, list of values=>labels * $multiple - if non-empty, select multiple * $size - number of rows to show if multiple * returns: string, HTML (i.e. for use in echo or print statement) */ function formDropDown($name,$value,$labels,$multiple = '',$size ='') { $html = '<SELECT NAME="' .$name . '"'; if(!empty($multiple)) { $html .= ' MULTIPLE'; } if(!empty($size)) { $html .= ' SIZE=' . $size; } $html .= ">\n"; $key = key($labels); while($key != "") { /* Check if the current value is in the $value variable */ if(is_array($value)) { if(in_array($key,$value)) { $selected = ' SELECTED'; } else { $selected = ''; } } else { if ($key == $value) { $selected = ' SELECTED'; } else { $selected = ''; } } $html .= "<OPTION VALUE=\"$key\"$selected>$labels[$key]\n"; next($labels); $key = key($labels); } $html .= "</SELECT>\n"; return $html; } ?> 4ÈÑ4 -- David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA Why build a wall round a cemetery when no-one wants to get in?

« previous php.general (#59650) next »