Re: Select list with PHP
| From: | Steve Edberg | Date: | Sun, 04 Feb 2001 00:41:40 +0000 |
| Subject: | Re: Select list with PHP | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-38017@lists.php.net to get a copy of this message | ||
At 3:24 PM -0700 2/3/01, Gerry wrote:
Hello: I'm trying to create dinamic color SELECT lists with php. I have my database set up as follows: Paint = good bucket = good weight = 100kg Colors = green, blue, red and here is my php: while ($row = mysql_fetch_array($sql_result)) { echo"<tr><td>"; echo $row["paint"]; echo"</td><td>"; echo $row["bucket"]; echo"</td><td>"; echo"<form action=\"http://www.\" method=\"POST\">";Replace this line:
$Color = $row["Color"];with this: $option = ''; $ColorArray = split(',' $row['Colors']); while (list(, $Color) = each($ColorArray)) { # yes, the comma in the list() should be there!
if ($Color == $Color) { $option .= "<OPTION value=\"$Color\" selected>$Color</OPTION>"; } else { $option .= "<OPTION value=\"$Color\">$Color</OPTION>"; }and add this here: }
echo"<select name=\"Colors\">"; echo "$option"; echo"</select>";For more information, see http://www.php.net/split Oh - and just in case the 'Colors' column is empty, you might want to do the while statement like - while (is_array($ColorArray) && (list(, $Color) = each($ColorArray))) { - since split() won't return an array if there's nothing to split. - steve
as you can see I end up with a select menu with this "green, blue, red" as the only option. is there a way I can tell php3 to break it down at "," so I can actually-- +--- "They've got a cherry pie there, that'll kill ya" ------------------+ | Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ | +-------------------------------------- FBI Special Agent Dale Cooper ---+have a drop down menuThanks ahead: Gerry Figueroa ---------------------------------- Modern Confucius: Man who run in front of car get tired. ----------------------------------