1

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:

output

how to prevent the repeated values in the dropdown ?

4
  • 1
    you can create an array to hold the values that you've already added and then check if the value is in the array. Commented Sep 19, 2016 at 11:27
  • But it has only two values which i m getting from the database i have to make them as selected in dropdown . Commented Sep 19, 2016 at 11:27
  • He is referring to $list->event. Also you mentioned sql database, use select where event not in [event list] Commented Sep 19, 2016 at 11:33
  • Yeah i understood.But both variables are getting from two tables like one table has all event list and another table has only two values . Commented Sep 19, 2016 at 11:37

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

No it did not work. Actually my $evlist has unique values only .But its happening because of $array has two values and two times its rotating the foreach loop for that its giving the values are getting repeated two times.
i have edited my answer,Please try it if it works for you.
some how i made it work .now no repeated values but one the first value getting selected ..Second value is not getting selected .any idea ?
1

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>

3 Comments

Yeah, this is working but only the first value is getting selected .how to make both values getting selected ? any idea ?
can you debug ? What is value in $array ?
Yes $array contains the values as i mentioned in the question .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.