好的,这就是我想要做的:
我有一个添加书籍表单,其中包含某些元素,例如标题、描述、作者和图像等。在添加表单中,我还有一些复选框,它们是用户想要放置书籍的类别。到目前为止,当用户单击在表单上添加图书按钮,所有输入均已验证,图书信息已插入数据库,图片上传成功。
一旦这一切都发生了,在模型中,我将返回刚刚添加到数据库中的记录的 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