vikela Posted May 13, 2010 Share Posted May 13, 2010 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 More sharing options...
phpchamps Posted May 13, 2010 Share Posted May 13, 2010 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 Link to comment https://forums.phpfreaks.com/topic/201609-multiselect-duplication/#findComment-1057648 Share on other sites More sharing options...
vikela Posted May 13, 2010 Author Share Posted May 13, 2010 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. Link to comment https://forums.phpfreaks.com/topic/201609-multiselect-duplication/#findComment-1057660 Share on other sites More sharing options...
vikela Posted May 13, 2010 Author Share Posted May 13, 2010 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: :o ;D :'( :'( Link to comment https://forums.phpfreaks.com/topic/201609-multiselect-duplication/#findComment-1057715 Share on other sites More sharing options...
vikela Posted May 14, 2010 Author Share Posted May 14, 2010 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> Link to comment https://forums.phpfreaks.com/topic/201609-multiselect-duplication/#findComment-1058169 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.