I am having a problem with an in_array function within a loop. Basically I have created a multiple select box pulling data from 1 field in a table. I then go to another table to find the matches. If the second table field matches the first table field, then the option should be highlighted within the select box.
To do this I created a loop to pull info from the second table (finding topic_codes matching specific id.). I then create the loop pulling all of the topic_codes from another table. While in this second loop I use in_array to match the topic_codes. (if they match, echo selected.) It sees the first code within the in_array, but when there are multiple codes from the first array, it does not see them all. I have tried placing the first array within the second loop, it again sees everything in the array (print_r), but when in in_array, it is only matching the first instance within that loop.
The code is below.
Sorry for the long explanation. Hopefully someone can help.
<select name="topic_code" multiple size="17"><?
$sp_topics = "select topic_code from speaker_topics where speaker_id=" . $result_array["speaker_id"];
$result4 = mysql_query($sp_topics) or die(mysql_error());
while ($speaker_topic_codes = (mysql_fetch_array($result4, MYSQL_ASSOC))) {
$final = $speaker_topic_codes;
// 01/09/02 seeing complete array print_r($final);
}
$topicquery = "select * from topic_codes ORDER BY topic_desc";
$topicresult = mysql_query($topicquery) or die(mysql_error());
while ($topicresult_array = mysql_fetch_array($topicresult, MYSQL_ASSOC)) {
echo "<option value=\"" . $topicresult_array["topic_code"] . "\"";
if (in_array($topicresult_array["topic_code"], $final)) {
echo " SELECTED";
}
echo ">" . stripslashes(htmlspecialchars($topicresult_array["topic_desc"])) . "</option>\n";
}
?></select>