使用Php mysql在多个选项中设置选定值

时间:2016-07-21 02:44:09

标签: php

  

我有这个代码用于在我的$ imeitransferserial中选择多个选择值这是值数组(2){[0] => string(15)" 869434022236534" [1] => string(15)" 869434022351051"在我的$ imeiserial我有数组(21){[0] => string(15)" 869434022236534" [1] => string(16)" 869434022353453" [2] => string(16)" 869434022351051" [3] => string(16)" 869434022234414" [4] => string(16)" 869434022342456"等等..我只想在我的选项中自动选择imeitransferserial的值多次尝试此代码但是所有项目都被选中。

 $trans=mysqli_query($link,"select * from transferinventory where ITEM_CODE_MX='OPP01-A11WGRY'");
    while($row2=mysqli_fetch_array($trans))
    {

     $imeitransfer=$row2["IMEI_MX"];
    $imeitransferserial = explode(',', $imeitransfer);
    var_dump($imeitransferserial);

    }
    $query=mysqli_query($link,"select * from inventory where ITEM_CODE_MX='OPP01-A11WGRY' AND BRANCH_CODE_MX='AC001'");
    while($row=mysqli_fetch_array($query))
    {

    $imei=$row["IMEI_MX"];
    $imeiserial = explode(',', $imei);

    }
    ?>
    <select id="tags" name='imei[]' style="width:300px;"  class="form-control select2-offscreen" onchange="getCount()" multiple>
    <?php
    foreach($imeiserial as $is){
        $imeicode = trim($is);
     if (in_array($imeicode,$imeitransferserial)) {

          $select = 'selected="selected" ';
          }
         if (!empty($imei)) {
            echo "<option  " . $select . "value='$imeicode'>".$imeicode."</option>";
        }
    }

        ?>

    </select>

1 个答案:

答案 0 :(得分:0)

问题是你在第一次迭代时设置了$select的值,并且当你得到错误

时从不清除该值
if (in_array($imeicode,$imeitransferserial))

您需要添加其他 -

if (in_array($imeicode,$imeitransferserial)) {
      $select = 'selected="selected" ';
 }
 else {
       $select = "";
 }