I have a multiple select box in one of my forms. I can insert the data into the database without a problem, however when the user recalls the form, I want to show them which values have been selected. I am able to do this with a single select, but not with a multiple select.
My select list is built from one table, and the users records come from another. I need a loop within a loop but for some reason I have only been able to get the first record to show as selected.
I have provided a simplified example below. This works, however it prints each select item several times.
<?php
$fruit[0] ="apple";
$fruit[1] ="orange";
$fruit[2] ="pear";
$fruit[3] ="bananna";
$fruit[4] ="grapes";
$fruit[5] ="cherries";
$fruit[6] ="kiwi";
$Displaymultselect[0] = "apple";
$Displaymultselect[1] = "orange";
$Displaymultselect[2] = "pear";
echo '<select name="fruit" multiple size="4">';
foreach ($fruit as $value)
{
foreach ($Displaymultselect as $newvalue)
{
echo '<option value="'.($value).'"'
.(($newvalue == ($value)) ? ' selected' : '')
.'> '.$value.' </option>';
}
}
echo "</select>";
?>