好的,这就是我想要做的事情:
我有一个添加书形式,其中包含标题,描述,作者和图像等某些元素。在添加表单中,我还有一些复选框,这些复选框是用户想要放入书籍的类别。到目前为止,当用户单击表单上的添加书按钮,验证所有输入,将书信息插入数据库,图像上传成功。
一旦发生这一切,模型中的 我将返回刚刚添加到数据库并将其插入另一个表的记录的book_id,如下所示:
$book_id = $this->db->insert_id();
$data = array(
'book_linktable_book_id' => $book_id,
);
$this->db->insert('book_linktable', $data);
我需要将book_category_id与book_id一起插入到另一个表中。 因此,对于用户选择的每个类别,请将book_id和book_category_id插入此另一个表格。
这是控制器代码:
function create_book(){
$this->form_validation->set_rules('title', 'Title', 'trim|xss_clean|required|max_length[50]');
$this->form_validation->set_rules('description', 'Description', 'trim|xss_clean|required|max_length[600]');
$this->form_validation->set_rules('exam_grade', 'Exam Grade', 'trim|xss_clean|required|max_length[10]');
$this->form_validation->set_rules('author', 'Author', 'trim|xss_clean|required|max_length[25]');
$this->form_validation->set_rules('edition', 'Edition', 'trim|xss_clean|required|max_length[15]');
$this->form_validation->set_rules('publisher', 'Publisher', 'trim|xss_clean|required|max_length[30]');
$this->form_validation->set_rules('price', 'Price', 'trim|xss_clean|required');
if ($this->form_validation->run() == FALSE)
{
$data = $this->style_model->admin_area();
$data['category_query'] = $this->admin_books_model->get_book_categories();
$data['page_title'] = 'Add New Book';
$this->load->view('admin/books/add_book', $data);
}
else
{
//upload the image and create thumbnail
$config['upload_path'] = APPPATH . 'uploads/';
$config['allowed_types'] = 'jpeg|jpg|gif|png';
$config['max_size'] = '1024';
$this->load->library('upload', $config);
$field_name = "image";
if( ! $this->upload->do_upload($field_name))
{
//failed to upload
$data = $this->style_model->admin_area();
$data['category_query'] = $this->admin_books_model->get_book_categories();
$data['page_title'] = 'Add New Book';
$this->form_validation->set_message("image", "Image size too large");
$this->load->view('admin/books/add_book', $data);
}
else
{
//image uploaded
$data = $this->input->post('category');
$this->load->view('admin/category', $data);
/*$upload_data = $this->upload->data('file_name');
$image_name = $upload_data['file_name'];
$this->admin_books_model->add_book($image_name);
$this->session->set_flashdata('msg', 'New Book Created');
redirect('admin_books');*/
}
}
} //function create_book()
以下是型号代码:
function add_book($image_name){
$data = array(
'book_name' => $this->input->post('title'),
'book_description' => $this->input->post('description'),
'book_grade' => $this->input->post('exam_grade'),
'book_author' => $this->input->post('author'),
'book_edition' => $this->input->post('edition'),
'book_publisher' => $this->input->post('publisher'),
'book_price' => $this->input->post('price'),
'book_image' => $image_name,
);
$this->db->insert('books', $data);
$book_id = $this->db->insert_id();
//for each category selected, getthe id and insert into link table
$data = array(
'book_linktable_book_id' => $book_id,
);
$this->db->insert('book_linktable', $data);
} //function add_book()
以下是视图中的表单代码:
<div id="left_form_content">
<?php echo form_open_multipart('admin_books/create_book'); ?>
<?php echo form_label('Title') . "<br />"; ?>
<?php echo form_input('title', set_value('title')); ?>
<?php echo form_error('title') . "<br />"; ?>
<?php echo form_label('Exam Grade') . "<br />"; ?>
<?php echo form_input('exam_grade', set_value('exam_grade')); ?>
<?php echo form_error('exam_grade') . "<br />"; ?>
<?php echo form_label('Edition') . "<br />"; ?>
<?php echo form_input('edition', set_value('edition')); ?>
<?php echo form_error('edition') . "<br />"; ?>
<?php echo form_label('Author') . "<br />"; ?>
<?php echo form_input('author', set_value('author')); ?>
<?php echo form_error('author') . "<br />"; ?>
<?php echo form_label('Publisher') . "<br />"; ?>
<?php echo form_input('publisher', set_value('publisher')); ?>
<?php echo form_error('publisher') . "<br />"; ?>
</div>
<div id="right_form_content">
<?php echo form_label('Description') . "<br />"; ?>
<?php echo form_textarea('description', set_value('description')); ?>
<?php echo form_error('description') . "<br />"; ?>
<?php echo form_label('Categories') . "<br />"; ?>
<?php
foreach($category_query as $data) {
echo "<input type='checkbox' name='category[]' value='" . $data->book_category_id . "'>" . $data->book_category_name . "</input>" . "<br />";
}
?>
<?php echo form_error('categories') . "<br />"; ?>
<?php echo form_label('Image') . "<br />"; ?>
<?php echo form_upload('image', set_value('image')); ?>
<?php echo form_error('image') . "<br />"; ?>
<?php echo form_label('Price') . "<br />"; ?>
<?php echo form_input('price', set_value('price')); ?>
<?php echo form_error('price') . "<br />"; ?>
<?php echo form_submit('submit', 'Add Book'); ?>
<?php echo form_close(); ?>
</div>
<div class="clear"></div>
提前感谢您的帮助,
汤姆
编辑:
以下是数据库表:
Books
book_id
book_title
book_description
book_author
book_grade
book_edition
book_publisher
book_image
book_price
book_categories
book_category_id
book_category_name
book_linktable
book_linktable_book_id
book_linktable_book_category_id
答案 0 :(得分:0)
你似乎与你想要完成的事情非常接近。您只需要遍历类别ID的数组并插入如下行:
$categories = $this->input->post('category');
foreach ($categories as $category_id)
{
$this->db->insert('book_linktable', array(
'book_linktable_book_id' => $book_id,
'book_linktable_book_category_id' => $category_id
));
}
或者,您也可以使用insert_batch()
方法。
$data = array();
foreach ($categories as $category_id)
{
$data[] = array('book_linktable_book_id' => $book_id,
'book_linktable_book_category_id' => $category_id
);
}
$this->db->insert_batch('book_linktable', $data);