1

I am using jquery-select2 for multiple option select.

I have a form where user can add two options from select2 select field and it is get inserted in to mysql as comma separated value. It is working ok.

Now if user want to edit it, with following code in edit_work.php, he is seeing each tag value twice in select option.

I have two mysql databases..... one is research_tag and second is research_work. During first upload user get tag values from research_tag and it get inserted into research_work table.

Coding I have tried in edit_work.php is :

<div>Tags :<br><font style="font-size:12px;">Select Tags Or Keywords For Your Research Work</font></div>
<div class="form-field-input">
<?
$tags = $workdataedit['tags']; // retrives values added by user during first upload
$tagarray = explode(',', $tags);

$tagsquery = mysql_query("select * from reseach_tags order by tags ASC");
?>

<select class="js-example-basic-multiple" multiple="multiple" name="tags[]" id="tags[]" required>
<? 
while ($tagdata = mysql_fetch_array($tagsquery)){ 
    foreach ($tagarray as $item){?>

    <option value="<?=$tagdata['tags']?>" <?if ($item == $tagdata['tags']){?> selected="selected"<?}?>><?=$tagdata['tags']?></option>
     <?}}?>
</select>

<script type="text/javascript">
$(".js-example-basic-multiple").select2({

  maximumSelectionLength: 2
});
</script>
</div>   

How to get option values Only Once in Dropdown ?

Image :

enter image description here

1 Answers1

0

First you can make your query more efficient by only selecting what you actually want from each row. You only seem to use the tags column.

Then to remove the duplications use distinct like this

Also if you use the newer foreach (): .... endforeach; and while (): .... endwhile; code format it will be easier to read your code when you eventually come back to amend it.

Although I cannot see a reason for the foreach loop inside the while loop so this can probably be removed.

<div>Tags :<br><font style="font-size:12px;">Select Tags Or Keywords For Your Research Work</font></div>
<div class="form-field-input">

<?php
    // retrives values added by user during first upload
    $tags = $workdataedit['tags']; 
    $tagarray = explode(',', $tags);

    $tagsquery = mysql_query("select DISTINCT tags 
                              from reseach_tags 
                              order by tags ASC");
?>

<select class="js-example-basic-multiple" multiple="multiple" name="tags[]" id="tags[]" required>

<?php
    while ($tagdata = mysql_fetch_array($tagsquery)) :
        // This inner foreach appears to be redundant  
        //foreach ($tagarray as $item) :
?>

<option value="<?=$tagdata['tags']?>" <?if ($item == $tagdata['tags']){?> selected="selected"<?}?>><?=$tagdata['tags']?></option>
<?php
        // redundant loop removed
        //endforeach;
    endwhile;
?>
</select>

<script type="text/javascript">
$(".js-example-basic-multiple").select2({

  maximumSelectionLength: 2
});
</script>
</div>   

Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the PDO database extensions. Start here its really pretty easy

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149