用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4083|回复: 2
收起左侧

[HELP] 怎么样取得多个文本框并向数据库中插入多条数据

[复制链接]
发表于 2011-5-11 15:53:11 | 显示全部楼层 |阅读模式
本帖最后由 nymbian 于 2011-5-11 17:39 编辑

实现一个功能
自定义模板实现特定样式页面的输入输出
其实就是类似论坛中的自定义帖子样式

$type_id 类型
每个类型对应着 一个或多个 option
每个 option 会输出形如
PHP复制代码
<?php echo $row->title; ?><input type="<?php echo $row->type; ?>" name="<?php echo 'option[' . $row->identifier . ']'; ?>"  />
复制代码
的input
问题就是怎样取到 input的值和 input name 多条数据一起放到对应
content 和content_type的表里
[td]

controller
PHP复制代码
function insert() {
        if ($type_id = $this->input->post('type_id')) {
 
            $subject = array(
                'subject' => $this->input->post('subject')
            );
 
            $data = array(
                'post_author' => $this->session->userdata('staff_id'),
                'subject' => $this->input->post('subject'),
                'post_date' => date("Y-m-d H:i:s", strtotime("now")),
                'post_status' => 'Active',
                'post_type' => $type_id
            );
 
 
 
 
            $this->load->model('post_type_insert_model');
            $post_id = $this->post_type_insert_model->insert_type($subject, $data);
 
 
            $this->load->model('type_get_model');
            $query = $this->type_get_model->get_type_by_id($type_id);
            foreach ($query->result() as $row) :
 
                $identifier = $row->identifier;
                $option = 'option[' . $row->identifier . ']';
                $array = $this->input->post($option);
                //print_r($array);
                $content = array(
                    'content' => $array,
                    'content_type' => $identifier,
                    'post_id' => $post_id
                );
                $this->post_type_insert_model->insert_conent($content);
            endforeach;
 
        }
    }
复制代码

view
PHP复制代码
      <?php echo form_open('/post_type_insert/insert'); ?>
        <?php echo form_hidden('type_id', $type_id); ?>
 
 
   
 
 
            <?php foreach ($query->result() as $row) : ?>
 
             
                    <?php echo $row->name; ?>
             
                <?php echo $row->title; ?><input type="<?php echo $row->type; ?>" name="<?php echo 'option[' . $row->identifier . ']'; ?>"  />
       
        <?php endforeach; ?>
   
 
    <?php echo form_submit('', '提交'); ?>
复制代码

发表于 2011-5-11 15:57:08 | 显示全部楼层
回复 nymbian 的帖子

没弄明白你要干嘛。
你的文本框要是都是name="option[]"的话就可以用$this->input->post('option');来得到数组,循环插入就是了。
跟纯PHP一样的呀
 楼主| 发表于 2011-5-13 09:52:01 | 显示全部楼层
回复 jeongee 的帖子

多谢版主
改为
PHP复制代码
 $array = $this->input->post('option');
            foreach ($array as $key => $val) {
               
                $content[$key] = array(
                    'content' => $val,
                    'content_type' => $key,
                    'post_id' => $post_id
                );
                $this->load->model('post_type_insert_model');
                $this->post_type_insert_model->insert_content($content[$key]);
           
            }
复制代码

后问题解决

本版积分规则