【问题标题】:How can I implement insert_batch active record in codeigniter in inserting multiple values?如何在 codeigniter 中实现 insert_batch 活动记录以插入多个值?
【发布时间】:2016-12-10 18:26:22
【问题描述】:

我目前正在使用 CodeIgniters insert_batch 通过单击添加多个请求。

这是我的模特

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

这是我的控制器 如果($_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]);
        }

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

【问题讨论】:

  • “此代码不起作用”。你必须解释一下,什么不起作用?它不应该做什么?它不应该做什么?有没有错误?如果是,错误信息是什么?最终看到这个页面stackoverflow.com/help/mcve,因为它一定会帮助你。
  • 它在数据库中添加空格。
  • 空白记录,要么表示变量没有值,要么表示类型不正确……但是由于您没有提供相关代码,我无法进一步帮助您。
  • 您需要在 insert_batch 中将数组数组作为 $data 传递。因此,不要在 Loop 中调用模型,而是在 Loop 完成后调用它并将 $data 传递给它。它肯定会工作

标签: php codeigniter activerecord model controller


【解决方案1】:

考虑一下这个控制器的代码:

$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);

【讨论】:

    猜你喜欢
    • 2013-08-03
    • 1970-01-01
    • 2011-03-22
    • 2011-09-28
    • 1970-01-01
    • 2012-12-06
    • 2012-06-21
    • 1970-01-01
    • 2013-05-02
    相关资源
    最近更新 更多