CodeIgniter-将输入数组写入数据库 [英] CodeIgniter - Writing input array to database

查看:56
本文介绍了CodeIgniter-将输入数组写入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一组输入字段中获取数据并将其写入数据库。我以前从未使用过数组,但这是我的代码。它基于我只是一个输入值时的处理方式。我知道这是错误的,但是我不知道下一步该怎么做。有任何想法吗?

I am trying to grab the data from an array of input fields and write them to a database. I have never worked with arrays before but this is the code I have. It is based off what I would do if it was just a single input value. I know this is wrong but I have no idea what to try next. Any ideas? Thanks.

//view
<input type="text" name="assignments[]">
<input type="text" name="hours[]">

<input type="text" name="assignments[]">
<input type="text" name="hours[]">

<input type="text" name="assignments[]">
<input type="text" name="hours[]">

<input type="text" name="assignments[]">
<input type="text" name="hours[]">

<input type="text" name="assignments[]">
<input type="text" name="hours[]">

//controller
$assignments = $this->input->post('assignments', TRUE);
$hours = $this->input->post('hours', TRUE);

$this->load->model('create', 'create_model');
$this->create_model->create_projects($assignments, $hours);

//model
public function create_projects($assignments, $hours) {

    $data = array(
        'assignments' => $assignments,
        'hours' => $hours,
        );
    $this->db->insert('projects', $data);
}


推荐答案

感谢答案。最终最适合我的是这个。

Thanks the answers. What ended up working best for me was this.

//controller
$assignments = $this->input->post('assignments');
$hours = $this->input->post('hours');

$this->load->model('create', 'create_model');

foreach($assignments as $key => $n){
    $this->create_model->create_projects($n, $hours[$key]);
}

这篇关于CodeIgniter-将输入数组写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆