I use the following 

Looks a bit unwieldy to call, but does everything I need.

eg, say I have a table called foo with ID, Name

print OptionCreate ("ID","returnID", "foo order by Name", "","Name");

will create me a drop down


print OptionCreate ("ID","returnID", "foo order by Name", "1","Name");

will create me a drop down with item 1 selected.

Hope this helps.



//OptionName = field to list, passedname=option Select name
//tablename= table name , selected value = option select selected value if
found

//NOTE THAT THE DB open MUST have been already made in the calling
routine!!!
//eg    $db = mysql_connect($db_domain, $db_user,$db_password);
//      mysql_select_db($db_databasename,$db);
        
function OptionCreate ($optionname,$passedname, $tablename,
$selectedvalue="",$optiontext="") {
        if ($optiontext=="")
                $sqlstring = "select $optionname from $tablename";
        else 
                $sqlstring = "select $optionname, $optiontext from
$tablename";

        
        global $db;
        $result=mysql_query($sqlstring,$db);
        $rows = mysql_num_rows ($result);
        
        $tmpOut = "<select name=\"$passedname\" size=1>";       
        for ($count=0;$count <$rows; $count++) {
                $tmpOut = $tmpOut . "<option value=\"" .
MYSQL_RESULT($result,$count,$optionname) . "\"";
                
                //Select item if passed variable is found
                if ($selectedvalue ==
MYSQL_RESULT($result,$count,$optionname))
                        $tmpOut=$tmpOut . "selected";
                if ($optiontext=="")
                        $tmpOut = $tmpOut . ">" .
MYSQL_RESULT($result,$count,$optionname) . "</option>";
                else
                        $tmpOut = $tmpOut . ">" .
MYSQL_RESULT($result,$count,$optiontext) . "</option>";
        }
        $tmpOut = $tmpOut . "</select>";
        return ($tmpOut);
}

-----Original Message-----
From: David Robley [mailto:[EMAIL PROTECTED]]
Sent: July 26, 2001 9:02 AM
To: CGI GUY; [EMAIL PROTECTED]
Subject: Re: [PHP] html form question


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?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to