如果数据与数据库相同,请选中该复选框

时间:2014-10-11 15:43:08

标签: php html explode checked

无法找出问题所在,例如在开始时我添加患者详细信息,我选择过敏和哮喘并存入数据库,但我想编辑细节,它只检查哮喘(最后一个值)...请帮助我找到答案谢谢。

<tr>
<td>Past Medical Records</td>
<td><?php 
    $DiseaseSplit = $medicalRec['Diseases'];
    $array = explode(", ",$DiseaseSplit);
    foreach ($array as $item) {
    echo "<li>$item</li>";
}
?></td></tr>
<tr><label class="q" for="q1"></label>    
<td><input name="q1[]" type="checkbox" value="NONE" <?php if($item == "NONE") { echo 'checked=\"checked\"';}?>>None</td></tr>

<tr>
<td><input name="q1[]" type="checkbox" value="ALLERGIES" <?php if($item == "ALLERGIES") { echo 'checked=\"checked\"';}?>>Allergies</td>
<td align="left"><input name="q1[]" type="checkbox" value="BLOOD DYSCRASIAS" <?php if($item == "BLOOD DYSCRASIAS") { echo 'checked=\"checked\"';}?>>Blood Dyscrasias</td></tr>

1 个答案:

答案 0 :(得分:0)

您在循环的每次迭代中都会覆盖$item

我会尝试制作一个哈希映射,以查看应显示/隐藏的值:

<tr>
<td>Past Medical Records</td>
<td><?php 
    $data = array();        

    $DiseaseSplit = $medicalRec['Diseases'];
    $array = explode(", ",$DiseaseSplit);
    foreach ($array as $item) {
        $data[$item] = true;
        echo "<li>$item</li>";
    }
?></td></tr>
<tr><label class="q" for="q1"></label>    
<td><input name="q1[]" type="checkbox" value="NONE" <?php if(isset($data["NONE"])) { echo 'checked=\"checked\"';}?>>None</td></tr>

<tr>
<td><input name="q1[]" type="checkbox" value="ALLERGIES" <?php if(isset($data["ALLERGIES"])) { echo 'checked=\"checked\"';}?>>Allergies</td>
<td align="left"><input name="q1[]" type="checkbox" value="BLOOD DYSCRASIAS" <?php if(isset($data["BLOOD DYSCRASIAS"])) { echo 'checked=\"checked\"';}?>>Blood Dyscrasias</td></tr>