【问题标题】:Inserting row for every value of array(checkbox value) Code igniter为数组的每个值插入行(复选框值) Codeigniter
【发布时间】:2016-07-05 04:00:57
【问题描述】:

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

<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>

如果我 print_r 它已经在一个数组中有它的值,例如:

Array ( [0] => 2 [1] => 3 [2] => 6 [3] => 7 [4] => 4 [5] => 5 [6] => 1 )

现在我想要实现的是对于每个检查的值,它将进入数据库。例如,我只为组 POSTVALUE 选择了下面的值,我在 input type="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';

【问题讨论】:

    标签: php mysql arrays codeigniter


    【解决方案1】:

    您的模型应如下所示

    你可以尝试简单的插入

    function insert_checkbox($array){
        foreach ($array as $row){
          $data = ['column'=>$row];
          $this->db->insert('table', $data); 
        }
    }
    

    你也可以试试insert_batch()

    function insert_checkbox($array){
        $data = [];
        foreach ($array as $row){
          $data[] = ['column'=>$row];
        }
        $this->db->insert_batch('table', $data); 
     }
    

    查看文档here

    【讨论】:

    • 如果我使用它,我的控制器会是什么样子?只是正常的方式?
    • $this-&gt;My_Model-&gt;function($array); ??
    • 是的。只是调用函数的正常方式。在您的控制器中:$this-&gt;my_model-&gt;insert_checkbox($array);
    • 如果我想为数组中的每个项目插入另一个值到数据库中
    • 我能做吗$data[] = ['column' =&gt; $this-&gt;input-&gt;post('value'), 'column'=&gt;$row];
    【解决方案2】:

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

    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;
        }
    }
    

    【讨论】:

    • 应该是$n = 1 还是$n = 0,因为数组以 0 开头,对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多