1

I am using CodeIgniter, I have bootstrap multiple select dropdowns. I am getting all the drop-down name in the dropdown list.

Now I am on the edit page, I have to display the selected value in the drop-down.

Would you help me out in this issue?

I have data in a database like

venue_id
1,4
5,6,7
1
10,15,4,9

view

<select name="venue_id[]" id="venue_id"  multiple="multiple" class="selectpicker form-control">
<?php
  foreach($venue as $list){
    echo '<option value="'.$list->venue_id.'">'.$list->venue.'</option>';
  }  
?>
user9437856
  • 2,360
  • 2
  • 33
  • 92
  • 1st. take all venue_id`s in array & remove duplicate values, then by mappint this array with $venue display option... 2nd . to display selected values... check new venue id array with venue in – Ashu Aug 30 '18 at 11:27
  • hope you will get the answer form this [enter link description here](https://stackoverflow.com/questions/18733545/selected-value-get-from-db-into-dropdown-select-box-option-using-php-mysql-error) – anand sh Aug 30 '18 at 12:03
  • @anandsh, Thanks for the reply, Your link is only for single select dropdown and my question is how to display the multiple selected – user9437856 Aug 31 '18 at 00:37

1 Answers1

1
<select name="venue_id[]" id="venue_id"  multiple="multiple" class="selectpicker form-control">
<?php
$selected_array = explode(',',$current_venue_id);
  foreach($venue as $list){
  $mark_as_select = (in_array($list->venue_id,$selected_array)) ? 'selected' : NULL;
    echo '<option value="'.$list->venue_id.'" '.$mark_as_select.'>'.$list->venue.'</option>';
  }  
?>

$current_venue_id which comes from the database.

Example $current_venue_id = 1,4 or $current_venue_id = 5,6,7 or

$current_venue_id = 1 or $current_venue_id = 10,15,4,9

CHITRASEN
  • 26
  • 4
  • Thanks for the reply, I just want to know what is the $current_venue_id? Is this venue id which comes from the database? – user9437856 Aug 31 '18 at 07:52
  • Yes it is venue id which comes from the database. Example $current_venue_id = 1,4 or $current_venue_id = 5,6,7 or $current_venue_id = 1 or $current_venue_id = 10,15,4,9 – CHITRASEN Sep 01 '18 at 07:04
  • Yes, It's working perfectly for me. Thanks, @CHITRASEN. Please add your last comment in the Answer for future reference. – user9437856 Sep 02 '18 at 10:00