Hi all,
My first thread here, and I'm sure it is something very basic that I'm missing.
I have a multi-select box that is populated from the database. When a user selects 1 or more items in this box I need to get the associated data from my database and then present it in two separate cells.
Here is what the database looks like.
My problem is that I can not get the $rating for each field 'nitrile' it is only returning the last value for in the foreach loop and assigning to every cell.
Here is my code:
<?
$con = mysql_connect("localhost","xxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xxx", $con);
$sql="SELECT * FROM chemicals";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$fluid=$row["chemical"];
$rating=$row["nitrile"];
$options.="<OPTION VALUE=\"$fluid\">".$fluid.'</option>';
}
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
<table width="850" border="1">
<tr valign="top">
<th width="350" scope="row"><div align="left">
<select name="test[]" size="7" multiple="multiple" class="select1">
</option>
<?php echo $options?>
</select>
</div></th>
<td width="375" rowspan="2"><div align="center"><strong>Chemical Selected</strong></div></td>
<td width="103" rowspan="2"><div align="center"><strong>Nitrile Rating</strong></div></td>
</tr>
<tr>
<th width="350" scope="row"><div align="right">
<input type="reset" />
<input type="submit" value="submit" name="submit" />
</div></th>
</tr>
<?php
$color_array = array("x" => "white", "1" => "green", "2" => "blue", "3" => "yellow", "4" => "red" );
if (isset($_POST['test']) && !empty($_POST['test']))
{
foreach ($_POST['test'] as $t){
echo "<tr valign='top'>" . "<td>" . "</td>";
echo "<td align='center'>" . $t . "</td>";
echo "<td align='center' bgcolor=" . $color_array[$rating] .">" . $rating . "</td>" . "</tr>";
}
}
mysql_close($con);
?>
</table>
</div></th>
</form>
</body>
</html>
An example of this not working right can be seen here.
Your input is appreciated.