php mysql multi-select box update

I am having trouble updating a multi-select box in php.  I can show the values that need to be selected.  I can show the multi-select box with all of the applicable values.  I am having trouble getting the options selected when the page first loads.
The selected values are stored in a string that I broke apart with explode.  The entire option list comes from an array from mysql.
Take a look at the code I have been playing with and let me know what you think.
$dealers_type = $_GET['dealers_type']; 
$query="SELECT * FROM department";
$deptArray = array();
while($row = mysql_fetch_array($result))
	$a1 = array($row["department_name"] => $row["department_id"] );
	$deptArray = array_merge($a1, $deptArray);
$deptArray2 = array_flip($deptArray);
echo '<select name="test">';
$str = $dealers_type;
$str2 = substr($str, 0, -1);
$data = explode("~",$str2);
foreach($data as $k => $v)
	echo '<option>'.$deptArray2[$v].'</option>';
echo '</select><br>';
@mysql_select_db($db) or die( "Unable to select database");
$sql = "SELECT * FROM department ORDER BY department_name";
$result = mysql_query($sql);
echo '<select name="ud_dealers_type[]" size="5" multiple="multiple"><option value=""></option>';
while ($row=mysql_fetch_array($result)) {
$selected = '';
$selected = ' selected';
echo '<option value="'.$row["department_id"].'" '.$selected.'>'.$row["department_name"].'</option>';
echo '</select>';

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roger BaklundCommented:
Try something like this:
while ($row=mysql_fetch_array($result)) {
  echo '<option value="'.$row["department_id"].'" '.
       (in_array($row["department_name"],$data) ?
         'selected="selected"' : '').

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newbe101Author Commented:
right on cxr... I just made one little change and it works perfect!
(in_array($row["department_name"],$data) ?
(in_array($row["department_id"],$data) ?

Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.