Populating Multiple Selection ListBox with data from 2 differnet tables
<p>So here is what i am trying to do. I have a table that contains a list of companies. I have another table that contains a list of companies for a specific employee. here are the tables</p>

<p><strong>companies</strong> <BR>
company_id <BR>
name</p>

<p><strong>employees_companies</strong> <BR>
empcomp_id <BR>
employee_id <BR>
company_id </p>

<p>Currently i have a list box that is populated with the list of all companies. When a user sets up a new employee they can choose one or more companies from the list box. When the form is submitted i create an array of all the selected companies, loop through them and add new records to the employees_companies table. This all works great. The problem i am having is on the form where a user can edit these companies. First i populate the list box with a list of all companies. Next i need to have the companies highlighted that are listed in the employees_companies table, for this user. Right now only one record is highlighted no matter how many were returned in the query. </p>

<p>Here is the code to get a list of all companies and the employees companies</p>

<pre><code>// get list of all companies
mysql_select_db($database_freight, $freight);
$query_rsCompanyList = "SELECT * FROM companies ORDER BY scac ASC";
$rsCompanyList = mysql_query($query_rsCompanyList, $freight) or die(mysql_error());
$row_rsCompanyList = mysql_fetch_assoc($rsCompanyList);
$totalRows_rsCompanyList = mysql_num_rows($rsCompanyList);

// get employees companies
$colname_rsEmployeeCompanyList = "17";
if (isset($_GET['employee_id'])) {
  $colname_rsEmployeeCompanyList = $_GET['employee_id'];
}
mysql_select_db($database_freight, $freight);
$query_rsEmployeeCompanyList = sprintf("SELECT * FROM employees_companies WHERE employee_id = %s", GetSQLValueString($colname_rsEmployeeCompanyList, "int"));
$rsEmployeeCompanyList = mysql_query($query_rsEmployeeCompanyList, $freight) or die(mysql_error());
$row_rsEmployeeCompanyList = mysql_fetch_assoc($rsEmployeeCompanyList);
$totalRows_rsEmployeeCompanyList = mysql_num_rows($rsEmployeeCompanyList);
</code></pre>

<p>Here is the form</p>

<pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id="frmAdd" name="frmAdd" method="post" action="test.php"&gt;
  &lt;select name="company_id[]" size="4" multiple="multiple" id="company_id[]"&gt;
    &lt;?php do { ?&gt;
    &lt;option value="&lt;?php echo $row_rsCompanyList['company_id']?&gt;"
    &lt;?php
        do {
            if (!(strcmp($row_rsCompanyList['company_id'], $row_rsEmployeeCompanyList['company_id']))) {echo "selected=\"selected\"";}
        } while ($row_rsEmployeeCompanyList = mysql_fetch_assoc($rsEmployeeCompanyList)); ?&gt;&gt;&lt;?php echo $row_rsCompanyList['name']?&gt;&lt;/option&gt;
    &lt;?php
} while ($row_rsCompanyList = mysql_fetch_assoc($rsCompanyList));
  $rows = mysql_num_rows($rsCompanyList);
  if($rows &gt; 0) {
      mysql_data_seek($rsCompanyList, 0);
      $row_rsCompanyList = mysql_fetch_assoc($rsCompanyList);
  }
?&gt;
  &lt;/select&gt;
  &lt;input type="submit" name="button" id="button" value="Submit" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>===============================================================================
almost got it, but there is just one issue. The company name is not populated in the listbox. I noticed your code didn't have the closing option tag. i added it and entered $company_id for the value, so it is populating the listbox with the company_id, but how would i echo the company name instead.</p>

<p>Here is where i am creating the arrays</p>

<pre><code>// get list of all companies
mysql_select_db($database_freight, $freight);
$query_rsCompanyList = "SELECT company_id, name FROM companies ORDER BY scac ASC";
$rsCompanyList = mysql_query($query_rsCompanyList, $freight) or die(mysql_error());

$all_company_ids = array();
while ($row_rsCompanyList = mysql_fetch_array($rsCompanyList)) {
  $all_company_ids[] = $row_rsCompanyList['compnay_id'];
}

// get list of all employees companies
$colname_rsEmployeeCompanyList = "17";
if (isset($_GET['employee_id'])) {
  $colname_rsEmployeeCompanyList = $_GET['employee_id'];
}
mysql_select_db($database_freight, $freight);
$query_rsEmployeeCompanyList = sprintf("SELECT company_id FROM employees_companies WHERE employee_id = %s", GetSQLValueString($colname_rsEmployeeCompanyList, "int"));
$rsEmployeeCompanyList = mysql_query($query_rsEmployeeCompanyList, $freight) or die(mysql_error());

$emp_company_ids = array();
while ($row_rsEmployeeCompanyList = mysql_fetch_array($rsEmployeeCompanyList)) {
  $emp_company_ids[] = $row_rsEmployeeCompanyList['compnay_id'];
}
</code></pre>

<p>Here is the form option</p>

<pre><code>&lt;?php foreach($all_company_ids as $company_id): ?&gt;
  &lt;option value="&lt;?php echo $company_id; ?&gt;" &lt;?php if(in_array($company_id, $emp_company_ids)) echo "selected=\"selected\""; ?&gt;&gt;&lt;?php echo $company_id; ?&gt;&lt;/option&gt;
&lt;?php endforeach; ?&gt;
</code></pre>
