buon giorno ragazzi ,volevo chiedervi , io sto usando un modulo select multiplo , dove estraggo da una tabella del database l id e il nome delle categorie , ho anche una tabella nel database dove associo idcategorie e idutente .... come fare che siano selezionati quelli associati ? ragazzi e un casino ma mi viene anche difficile da spiegare

Codice PHP:
<select name="departmentassigned[]" multiple="multiple" size="5" class="rcwswiftselect" style="width: 170px;"  >
                <?php
                
                 
foreach ($deparassigned as $dep){
                        foreach(
$departfromid as $key ){   
                                             
?>
                <option value="<?php echo $dep['departid']; ?><?php if($dep['departid'] == $key['departmentid']){echo'selected';}?>  ><?php echo $dep['departname']; ?></option>
                <?php  
                    
}
                }
?>
              </select>
Codice PHP:
function ViedDepartmentFromId($staffid){

global 
$dbcore ;
if( empty(
$staffid))
{
return 
false;
}
$dep $dbcore->getlist("select t. *,tass. departmentid   from tech as t  left join techassigns as tass on  t.staffid = tass.staffid   where t. staffid =  ".$staffid."  limit 1");

if( !
$dep ){return false;}

return 
$dep;