php mysql multi-select box update

Posted on 2008-11-07
Last Modified: 2013-12-13
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

Question by:newbe101
    LVL 39

    Accepted Solution

    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


    Author Comment

    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Creating and Managing Databases with phpMyAdmin in cPanel.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now