2016-10-16 52 views
1

複選框有更新表單,所以我需要,如果某些複選框輸入已經根據MySQL數據進行檢查,那麼它會顯示檢查。複選框檢查MySQL數據的逗號(,)

<?php 
$statement = $db->prepare("SELECT leavingEquipment FROM orderform WHERE orderNo='$orderNo'"); // here are the checked data 
$statement->execute(); 
$result = $statement->fetchAll(PDO::FETCH_ASSOC); 
    foreach($result as $row4){ 
     $leavingEquipment = explode(", ",$row4['leavingEquipment']); // Here I use explode I check this with var_dump 
     $statement = $db->prepare("SELECT * FROM equipment"); // here all equipment 
     $statement->execute(); 
     $result = $statement->fetchAll(PDO::FETCH_ASSOC); 
      foreach($result as $row3) { 
       $leavingEquipment3=$row3['equipmentTitle']; 
       ?> 
      <input type="checkbox" name="leavingEquipment[]" value="<?php echo $row3['equipmentTitle']; ?>" <?php if($leavingEquipment==$leavingEquipment3) echo 'checked'; ?>><label class="control-label"><?php echo $row3['equipmentTitle']; ?></label> 
       <?php 
      } 
    } 
?> 

Var_dum輸出這樣的: 陣列(2){[0] =>串(9) 「藍牙」 1 =>串(6) 「殼體」}

它顯示了這樣的: enter image description here

但我需要這樣的:提前 enter image description here

感謝。

回答

2

看到這裏,

<input ... name="leavingEquipment[]" } value="<?php echo $row3['equipmentTitle']; ?>" <?php if($leavingEquipment==$leavingEquipment3) echo 'checked'; ?>> ... 
            ^a lone closing bracket         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

你比較數組$leavingEquipment用字符串$leavingEquipment3

取而代之的是使用in_array()函數來檢查複選框是否已經被檢查,即設備是否已經在陣列中。所以你的內在foreach循環應該是這樣的:

// your code 

foreach($result as $row3) { 
    $leavingEquipment3=$row3['equipmentTitle']; 
    ?> 
    <input type="checkbox" name="leavingEquipment[]" value="<?php echo $row3['equipmentTitle']; ?>" <?php if(in_array($leavingEquipment3, $leavingEquipment)){ echo " checked='checked'"; } ?>> 
    <label class="control-label"><?php echo $row3['equipmentTitle']; ?></label> 
    <?php 
} 

// your code 
+0

長閉括號是錯誤的。 –

+0

感謝它的工作..很多很多謝謝 –

+0

@AmranurRahman你非常歡迎! :-) –