Jump to content

multiselect duplication


vikela

Recommended Posts

Here is the scenario..I have a database with 2 tables 1 for users contact details and the other has user comments and preferences.In the table of comments I have one field I want to contain multiple values.However, I am able to upload the multiple values(through serialize and unserialize) and retrieve them but the problem I am now having is on editing the multiple values.I am using multiple select.The following code is duplicating the users in the select menu.

 

<select multiple="true" name="user_id[]" id="the_user_id"  size="6">
<?php 
$finder=unserialize($info[0]['user_id']);//from comments table
foreach($finder as  $value)
foreach($allusers as $rows){
$bigg=($rows['user_id']==$value)?'selected':NULL;
echo '<option value ="'.$rows['user_id'].'" '.$bigg.'>'.$rows['user_name'].'</option>';

}

?>
</select>

Link to comment
https://forums.phpfreaks.com/topic/201609-multiselect-duplication/
Share on other sites

Try this ...

 

foreach($allusers as $rows){
	foreach($finder as  $value){
	$bigg=($rows['user_id']==$value)?'selected':NULL;
}
echo '<option value ="'.$rows['user_id'].'" '.$bigg.'>'.$rows['user_name'].'</option>';
}

 

 

I am assuming, allusers contains the information about all users and while finder array has the details of selected users

  Quote

Try this ...

 

foreach($allusers as $rows){
	foreach($finder as  $value){
	$bigg=($rows['user_id']==$value)?'selected':NULL;
}
echo '<option value ="'.$rows['user_id'].'" '.$bigg.'>'.$rows['user_name'].'</option>';
}

 

 

I am assuming, allusers contains the information about all users and while finder array has the details of selected users

 

Thanks.

Did try but its no longer duplicating however its showing only 1 selected option instead of say 3.

Have managed to get the other selected options shown but now the selected options are being repeated any workaround? I am still fresh on PHP

 

<select multiple="true" name="user_id[]" id="the_user_id"  size="6">
<?php 
$repeat;
$finder=unserialize($info[0]['user_id']);
foreach($allusers as $rows){
       foreach($finder as  $value){
       $bigg=($rows['user_id']==$value)?'selected':NULL;
   if($repeat!='<option value ="'.$rows['user_id'].'" '.$bigg.'>'.$rows['user_name'].'  </option>') {
     echo $repeat;
  $repeat='<option value ="'.$rows['user_id'].'" '.$bigg.'>'.$rows['user_name'].'  </option>'; 
   }
   }

}
?>
</select>

 

>:( >:(:shrug: :shrug: :o :o ;D ;D :'( :'(

 

Have managed to get the other selected options shown but now the selected options are being repeated any workaround.Whats the best way forward

 

<select multiple="true" name="user_id[]" id="the_user_id"  size="6">
<?php 
$repeat;
$finder=unserialize($info[0]['user_id']);
foreach($allusers as $rows){
       foreach($finder as  $value){
       $bigg=($rows['user_id']==$value)?'selected':NULL;
   if($repeat!='<option value ="'.$rows['user_id'].'" '.$bigg.'>'.$rows['user_name'].'  </option>') {
     echo $repeat;
  $repeat='<option value ="'.$rows['user_id'].'" '.$bigg.'>'.$rows['user_name'].'  </option>'; 
   }
   }

}
?>
</select>

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.