Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter上显示阵列数据复选框_Php_Codeigniter 3 - Fatal编程技术网

Php 在codeigniter上显示阵列数据复选框

Php 在codeigniter上显示阵列数据复选框,php,codeigniter-3,Php,Codeigniter 3,我有如下的表格数据,我想根据它的值在复选框中显示数据,数据是数组的形式,我很难应用它。因此,如果数据1不存在,则会出现错误 | ID | choice | | ____|________| | 1 | A | | 1 | B | | 3 | C | | 6 | A | | 6 | C | | 5 | A | | 5 | B | |

我有如下的表格数据,我想根据它的值在复选框中显示数据,数据是数组的形式,我很难应用它。因此,如果数据1不存在,则会出现错误

   | ID  | choice | 
   | ____|________|
   |  1  |    A   |
   |  1  |    B   |
   |  3  |    C   | 
   |  6  |    A   | 
   |  6  |    C   |
   |  5  |    A   |
   |  5  |    B   |
   |  5  |    C   |

 <input type="checkbox" name="choice[]" value="A"  <?php ($edit[0]['is'] == "A") ? print "checked": ''; ?> >
 <input type="checkbox" name="choice[]" value="B" <?php ($edit[1]['is'] == "A") ? print "checked": ''; ?> >
 <input type="checkbox" name="choice[]" value="C" <?php ($edit[2]['is'] == "A") ? print "checked": ''; ?>>
模型

function edit_data($id){
    $query = $this->db->select('choice')
                      ->from('my_table')
                      ->where('id', $id)
                      ->get();
    if ($query->num_rows() > 0) {
        foreach ($query->result_array() as $data) {
            $result[] = $data;
        }
        return $result;
    }                         
}   

当我选择只有2个或1个数据时出错,请先检查父项是否存在,然后再检查子项

 <input type="checkbox" name="choice[]" value="A"  <?php (array_key_exists(0, $edit) && $edit[0]['is'] == "A") ? print "checked": ''; ?>>
 <input type="checkbox" name="choice[]" value="B" <?php (array_key_exists(1, $edit) && $edit[1]['is'] == "A") ? print "checked": ''; ?>>
 <input type="checkbox" name="choice[]" value="C" <?php (array_key_exists(2, $edit) && $edit[2]['is'] == "A") ? print "checked": ''; ?>>
>

我想你应该在php的数组()中使用
函数

<?php 
    $checkboxArray = array(1, 2, 3, 4); // get from database or you can set it in your view
    $savedCheckedboxArray = $edit = array(2,4); // saved into database

    foreach($checkboxArray as $row) 
    { 
        $checked = "";
        if (in_array($row, $savedCheckedboxArray)) {
            $checked = "checked:checked";
        } ?>
    <input type="checkbox" name="choice[]" value="<?php echo $row;?>" <?php echo $checked ?> />
 <?php } ?>


感谢萨南·古利耶夫,我已经尝试过了,但还是做了一个循环,所以我希望一个输入复选框能够检查出它的数据是否存在$id@irwandwiyanto首先,如果DB没有结果,您应该返回空数组
return[]
。否则,
edit_data
的返回类型将无效。为了避免不存在错误,您可以这样改进您的条件:
array\u key\u exists(0,$edit)&&$edit[0]['is']==“A”
谢谢@nagen nayak,我以前在数组中使用过,但无法显示数组的值。我已经更新了我的答案,请查看,它将在您的代码中为您提供想法。
<?php 
    $checkboxArray = array(1, 2, 3, 4); // get from database or you can set it in your view
    $savedCheckedboxArray = $edit = array(2,4); // saved into database

    foreach($checkboxArray as $row) 
    { 
        $checked = "";
        if (in_array($row, $savedCheckedboxArray)) {
            $checked = "checked:checked";
        } ?>
    <input type="checkbox" name="choice[]" value="<?php echo $row;?>" <?php echo $checked ?> />
 <?php } ?>