RE: Code for drop down lists

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



	I would most definitely recommend pulling them from a DB table
rather than hard coding these into a page.  Here is a code snippet that I
use to do this on one of my pages:

mysql_select_db($Prod, $Prod);
$query_systems = "SELECT Name FROM systems ORDER BY Name ASC";
$systems = mysql_query($query_systems, $Prod) or die(mysql_error());
# $row_systems = mysql_fetch_assoc($systems);
$totalRows_systems = mysql_num_rows($systems);
$sys_list = "<select size=\"1\" name=\"system\">\n";
$sys_list .= "<option>System Name</option>\n";
$sys_list .= "<option>-----------</option>\n";
while($name = mysql_fetch_row($systems)) {
  $sys_list .= "<option>$name[0]</option>\n";
}
$sys_list .= "</select>\n";

<<  snip...HTML portion below >>

<td width="124" valign="top"><div align="right">System Name:</div></td>
      <td colspan="2" valign="top"> 
        <?=$sys_list?>

<< snip >>

	I also do this with Enum field selections using the following
function that someone passed on to me:

function getEnumOptions($table, $field) {
   global $finalResult;
   $finalResult = array();

   if (strlen(trim($table)) < 1) return false;
   $query  = "show columns from $table";
   $result = mysql_query($query);
   while ($row = mysql_fetch_array($result)){
        if ($field != $row["Field"]) continue;
        //check if enum type
        if (ereg('enum.(.*).', $row['Type'], $match)) {
            $opts = explode(',', $match[1]);
            foreach ($opts as $item)
                $finalResult[] = substr($item, 1, strlen($item)-2);
        }
        else
                return false;
   }
   return $finalResult;

	You can then use code similar to the first example to create your
dropdown list from Enum field values.  I hope this helps.

-----Original Message-----
From: Roland Perez [mailto:Roland.Perez@usdoj.gov]
Sent: Tuesday, February 11, 2003 11:03 AM
To: php-db@lists.php.net
Subject:  Code for drop down lists


I am a novice to PHP and am trying toget a survey with drop down lists.
I am stuck as to if I should connect to the DB (MySQL DB) to get the
values from a table or do I imbed them in the php file?

Thanks for any help in advance.
Roland Perez
roland.perez@usdoj.gov

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux