Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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 为数组的每个值(复选框值)插入行代码点火器_Php_Mysql_Arrays_Codeigniter - Fatal编程技术网

Php 为数组的每个值(复选框值)插入行代码点火器

Php 为数组的每个值(复选框值)插入行代码点火器,php,mysql,arrays,codeigniter,Php,Mysql,Arrays,Codeigniter,我有一个视图,它为数据库中的每一行输出一个复选框,它工作得非常好 <div class="box-body"> <div class="form-group"> <label class="col-sm-2 control-label col-sm-offset-2" >CheckBox:</label> <div class="col-sm-5"> <

我有一个视图,它为数据库中的每一行输出一个复选框,它工作得非常好

<div class="box-body">
    <div class="form-group">
        <label class="col-sm-2 control-label col-sm-offset-2" >CheckBox:</label>
            <div class="col-sm-5">
               <div class="form-group">
                 <?php foreach($content1 as $cb){ ?>
                    <div class="checkbox">
                      <label>
                        <input type="checkbox" name="h[]" value = "<?php echo $cb->USERCODE?>">
                        <?php echo $cb->DEPARTMENT.' '.$cb->USERTYPE?>
                      </label>
                     </div>
                <?php } ?>
             </div>
        </div>
    </div>
</div>
现在我想要实现的是,对于检查的每个值,它都将输入到数据库中。例如,我只为组POSTVALUE选择了下的值,我在输入类型=text中输入了该值

 Array ( 
[0] => 2 
[1] => 3 
[2] => 4  )
我的模型将进行查询

insert into table column , column1 values '2' , 'POSTVALUE';
insert into table column , column1 values '3' , 'POSTVALUE';
insert into table column , column1 values '4' , 'POSTVALUE';

提交值后,将该值从控制器传递给模型函数。 在控制器中使用此功能

public function form_action()
{

    $h= $this->input->post('h');
    $data=array(
    'postvalue' => $this->input->post('postvalue'),
    'h' => $h
    );
  $this->model_name->insert_value($data);
}
这是您的示例模型函数。这对你有帮助

public function insert_value($data)
{        
    $n =0;
    foreach($data['h'] as $rows) {
        $data[$n] = array(
        'column' => $rows,
        'column1' => $data['postvalue'],
        );
        $query = $this->db->insert('table_name',$data[$n]);
        $n++;
    }


    if($query)
    {

        return true;
    }
    else
    {
        return false;
    }
}
你的模型应该是这样的

您可以尝试一个简单的插入

function insert_checkbox($array){
    foreach ($array as $row){
      $data = ['column'=>$row];
      $this->db->insert('table', $data); 
    }
}
您也可以尝试使用insert_batch


查看文档

它应该是$n=1还是$n=0,因为数组以0开头,对吗?如果我使用它,我的控制器会是什么样子?只是普通的方式?$this->My_Model->function$array??对这是调用函数的正常方式。在控制器中:$this->my_model->insert_checkbox$array;如果我想在数组中的每个项目的数据库中插入另一个值,我可以执行$data[]=['column'=>$this->input->post'value','column'=>$row];
function insert_checkbox($array){
    foreach ($array as $row){
      $data = ['column'=>$row];
      $this->db->insert('table', $data); 
    }
}
function insert_checkbox($array){
    $data = [];
    foreach ($array as $row){
      $data[] = ['column'=>$row];
    }
    $this->db->insert_batch('table', $data); 
 }