如何在codeigniter中实现insert_batch活动记录以插入多个值?

时间:2016-12-10 18:26:23

标签: php codeigniter activerecord model controller

我目前正致力于使用CodeIgniters insert_batch单击添加多个请求。

这是我的模特

function add_request($data) {
            $this->db->insert_batch('requests',$data);
        }

这是我的控制器 if($ _ POST){

        $code = $this->input->post('code');
        $about = $this->input->post('about');
        $qnty = $this->input->post('quantity');
        $budget = $this->input->post('budget');
        $sched = $this->input->post('sched');

        for($i = 0; $i < count($code); $i++) {
                $data[$i] = array(
                    'code' => $code,
                    'description' => $_POST['desc'],
                    'qnty' => $qnty,
                    'budget' => $budget,
                    'sched' => $sched,
                    'from' => $this->session->userdata('user_id'),
                    'status' => 'Pending',
                    'about' => $about
                );
            $this->request->add_request($data[$i]);
        }
  

此代码不起作用它只添加空白记录。

1 个答案:

答案 0 :(得分:0)

考虑控制器的这段代码:

$data = array();
for($i = 0; $i < count($code); $i++) {//build the array
            $data[$i] = array(
                'code' => $code,
                'description' => $_POST['desc'],
                'qnty' => $qnty,
                'budget' => $budget,
                'sched' => $sched,
                'from' => $this->session->userdata('user_id'),
                'status' => 'Pending',
                'about' => $about
            );

    }
//$data will be a bidimentional array
//pass $data to the model after the looping is done, thus the array is complete
$this->request->add_request($data);