[PHP-DB] 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 [EMAIL PROTECTED] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Code for drop down lists
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 .= optionSystem 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=topdiv align=rightSystem 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:[EMAIL PROTECTED]] Sent: Tuesday, February 11, 2003 11:03 AM To: [EMAIL PROTECTED] Subject: [PHP-DB] 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 [EMAIL PROTECTED] -- 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
RE: [PHP-DB] Code for drop down lists
If the list is something that may change more than just coccasionally, then use the database field value. If its going to be really static, then hard code it into the web form. Now the code: ?php $drop_down_list = select name=HEI_ID; $link = mysql_connect ('localhost', 'user', 'password') // Connects to database server or die (Could not select db); mysql_select_db ('Heronsql') // opens database or die (Could not connect); $query=select distinct Nickname, HEI_ID from Customers where (Nickname '') order by Nickname; // sets up the find $result = mysql_query ($query); // runs the find $num = mysql_num_rows($result); //goes thru each row building the display for ($i = 0; $i $num; $i++) { $HEI = mysql_result($result, $i, Nickname); $HEI_ID = mysql_result($result, $i, HEI_ID); $drop_down_list .= option value=$HEI_ID$HEI/option; } mysql_close($link); $drop_down_list .= /select; echo $drop_down_list; // prints the display ? This works for me on both Linux and Win servers. Cheers George -Original Message- From: Roland Perez [mailto:[EMAIL PROTECTED]] Sent: 11 February 2003 5:03 pm To: Receipt Notification Requested Subject: [PHP-DB] 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 [EMAIL PROTECTED] -- 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
RE: [PHP-DB] Code for drop down lists
Always use a DB when you can, makes it much easier to change params. Here's a snippet you can use: ### SELECT BOX CODE ### echo ' tdSELECT NAME=change SIZE=1'; $sql = SELECT * from ai.reports order by name; $selections = get_rowset($conn_id,$sql); foreach($selections as $selection){ echo 'OPTION'; $reptmp = $selection['name']; if ($val == $reptmp){ echo ' SELECTED'; } echo '' . $selection['name']; } echo '/select/td'; ## CAVEATS: The get_rowset is a function we've written for retrieving an array from the DB The if $val == $reptmp is to check for whatshould be selected Gary Every Sr. UNIX Administrator Ingram Entertainment (615) 287-4876 Pay It Forward mailto:[EMAIL PROTECTED] http://accessingram.com -Original Message- From: Roland Perez [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 11, 2003 11:03 AM To: [EMAIL PROTECTED] Subject: [PHP-DB] 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 [EMAIL PROTECTED] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php