drop0118 发表于 2011-4-20 11:28:41

CI如何做循环插入数据?

FORM表单是这样的:

<form action="index/add" methed="post">

<input name="name[]"/><br />
<input name="type[]" /><br />
<input name="content[]" /><br />

<input name="name[]"/><br />
<input name="type[]" /><br />
<input name="content[]" /><br />

<input name="name[]"/><br />
<input name="type[]" /><br />
<input name="content[]" /><br />

<input name="name[]"/><br />
<input name="type[]" /><br />
<input name="content[]" /><br />

<input name="name[]"/><br />
<input name="type[]" /><br />
<input name="content[]" /><br />

</form>


这个内容的循环次数是不定的,现在有两个问题请教,首先HTML表单部分是不是这么写啊?第二个:CI这部分的ACTIVE RECORD怎么写?

jeongee 发表于 2011-4-20 13:15:12

跟纯PHP无差别啊,以前怎么写,现在还是怎么写

drop0118 发表于 2011-4-20 14:07:18

回复 2# jeongee


    :lol能写个代码示例片段吗?我不太会:'(

jeongee 发表于 2011-4-20 14:11:28

回复 3# drop0118


   你不用CI的时候怎么写的先放出来看看~

drop0118 发表于 2011-4-20 14:24:08

回复 4# jeongee


    呃不会···

huboo82 发表于 2011-4-20 14:30:22


$name = $this->input->post('name');
$type = $this->input->post('type');
$content = $this->input->post('content');
foreach($name as $key => $val)
{
    $data = array(
      'name' => $val,
      'type' => $type[$key],
      'content' = $content[$key]
    );
    $this->db->insert('table', $data);
}

drop0118 发表于 2011-4-20 14:41:41

回复 6# huboo82


    谢谢啦!:loveliness:

zhouli520 发表于 2011-4-20 15:26:57


huboo82 发表于 2011-4-20 14:30 http://codeigniter.org.cn/forums/images/common/back.gif


    这样会不会有个问题,就是如果,我有一个变量没填写,那么你循环出来的时候肯定会少一个变量,那么就变成了name变量本应为空的却填入了name变量的值

drop0118 发表于 2011-4-20 16:05:03

这样会不会有个问题,就是如果,我有一个变量没填写,那么你循环出来的时候肯定会少一个变量,那么就变 ...
zhouli520 发表于 2011-4-20 15:26 http://codeigniter.org.cn/forums/images/common/back.gif


    是啊!是个严重的问题啊!

rockics 发表于 2011-4-21 16:24:15

是啊!是个严重的问题啊!
drop0118 发表于 2011-4-20 16:05 http://codeigniter.org.cn/forums/images/common/back.gif
可以先判空,其实sql可以写成一条就行了,呵呵
页: [1] 2
查看完整版本: CI如何做循环插入数据?