怎么样取得多个文本框并向数据库中插入多条数据
本帖最后由 nymbian 于 2011-5-11 17:39 编辑实现一个功能
自定义模板实现特定样式页面的输入输出
其实就是类似论坛中的自定义帖子样式
$type_id 类型
每个类型对应着 一个或多个 option
每个 option 会输出形如
<?php echo $row->title; ?><input type="<?php echo $row->type; ?>" name="<?php echo 'option[' . $row->identifier . ']'; ?>"/>的input
问题就是怎样取到 input的值和 input name 多条数据一起放到对应
content 和content_type的表里
controller
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 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('', '提交'); ?>
回复 nymbian 的帖子
:(没弄明白你要干嘛。
你的文本框要是都是name="option[]"的话就可以用$this->input->post('option');来得到数组,循环插入就是了。
跟纯PHP一样的呀 回复 jeongee 的帖子
多谢版主
改为
$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]);
}
后问题解决
页:
[1]