Reputation: 807
I have two foreach loops one contains the event list and one contains the values that have been selected by the user from the database.
Here is my code:
<select class="form-control select2-select" autocomplete="off" name="event[]" multiple>
<option value="">--Select--</option>
<?php
$variable= $editlist['evname'];
$array = explode(',', $variable);
foreach($evlist as $list){
foreach($array as $value){
?>
<option value="<?php echo $list->event ?>"<?php if($list->event==$value) echo "selected";?>><?php echo $list->event ?>
</option>
<?php
}
}
?>
</select>
$evlist contains:
array:9 [▼
0 => "3rd Edition Healthscape Summit India-2016"
1 => "6th Edition RESCOM Summit - Middle East"
2 => "8th Edition Hotelier Summit Middle East-2016"
3 => "Smart Education India-2016"
4 => "15th Edition Design Mission India-2016"
5 => "16th Edition Design Mission Africa-2016"
6 => "1st Edition India Industrial Summit 2016"
7 => "Pharmac India-2016"
8 => "17th Edition Design Mission Saudi Arabia-2016"
]
here $array contains only two values like:
Array (
[0] => Smart Education India-2016
[1] => 6th Edition RESCOM Summit - Middle East
)
And my output is as follows:
how to prevent the repeated values in the dropdown ?
Upvotes: 1
Views: 934
Reputation: 968
please try it :
<select class="form-control select2-select" autocomplete="off" name="event[]" multiple>
<option value="">--Select--</option>
<?php
$variable= $editlist['evname'];
$array = explode(',', $variable);
foreach($evlist as $list){
?>
<option value="<?php echo $list->event ?>"
<?php
if(in_array($list->event, $array))
echo "selected";?>><?php echo $list->event ?>
</option>
<?php
}
?>
</select>
Upvotes: 1
Reputation: 1045
There are two options:
1) Make your array unique
<select class="form-control select2-select" autocomplete="off" name="event[]" multiple>
<option value="">--Select--</option>
<?php
$variable= $editlist['evname'];
$array = explode(',', $variable);
foreach(array_unique($evlist)as $list){ ?>
<option value="<?php echo $list->event ?>"<?php if(in_array($list->event,$array)) echo "selected";?> > <?php echo $list->event ?>
</option>
<?php
}
?>
</select>
2) In query add group by option value(i.e. event)
Upvotes: 1