Following code may help you
<html>
<body>
<form>
<!-- copy following php code at the place where you are populating your list box-->
<?php
/*
set $mywholearray from database
this array contains all master values to be shown in listbox
$mywholearray[0]['id']='pkvalue',
$mywholearray[0]['desc']='description',
*/
/*set $myselarray from database
this array contains only those values which are selected and stored in database
$myselarray[0]['id']='pkvalue'
*/
$elementstring ="\n<SELECT id='listbox' size=10 name='listbox' multiple>";
for ($i=0;$i<count($mywholearray);$i++)
{
$seltext="";
for($j=0;$j<count($myselarray);$j++)
{
if ($mywholearray[$i]['id']==$myselarray[$j])
{
$seltext=" selected ";
break;
}
}
$elementstring .= "\n";
$elementstring .= "<OPTION value='{$mywholearray[$i]['id']}' {$seltext}>";
$elementstring .="{$mywholearray[$i]['desc']}</OPTION>";
}
$elementstring .="\n</SELECT>";
echo $elementstring;
?>
</form>
</body>
</html>