I am working with a form that has a some 'select" fields that allow multiple selections. So the submitted data can obtain multiple values for certain field names. I put these results into an array and I have no problem running them through the database and coming up with the proper results.
The problem I'm having is that on the follow up page, I'm trying to show the select box with the fields selected as they were when the form was submitted. (the select box options are pulled from a database) This seemed a simple enough problem to deal using in_array within a while loop.
Here is how I set up the code.
On submission, the select box form field selections are put into an array:
$select_array ## the array would be of integers (1,3,5)
And the query to create the select options would be:
$query = "select * from select_list";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$select_id = $row['select_id'];
$select_desc = $row['select_desc'];
if (in_array($select_id, $select_array)) {
$selected = "selected";
}
else
{
$selected = "";
}
$select_options .= "<option value=\"$select_id\" $selected>
$select_desc</option>";
}
The problem I'm having is that it is only working for the first value of the array. In other words, no matter how many selections are made, only the first value is coming up as a match. As I said, I know the array has all of the values selected because it is working in the database query. (I've also printed the array out to make sure) So the array definitely has all of the values it should.
Is there something I'm not aware of about running in_array within a while loop?