如何解析从控制器到视图的值?当我从控制器调用book_title和book_category值时,视图页面中总是出现未定义变量错误。有人能帮我吗?谢谢
控制器(Book.php(:
function __construct(){
parent::__construct();
$this->load->model('md_book');
}
public function book_controller(){
$login_type = $this->session->userdata('login_type');
$page_data['login_type'] = $login_type;
$page_data['page_name'] = 'book';
$page_data['page_title'] = 'Book';
$page_data['page_function'] = __FUNCTION__;
$page_data['page_breadcrumb'] = generate_breadcrumb(array('Book'));
$data['book_title'] = $this->input->post('book_title');
$data['book_category'] = $this->input->post('book_category');
$this->md_book->addBook($data);
$this->load->view('index', $page_data);
}
模型(md_book.php(:
function addBook($data){
$this->db->insert('tb_books', $data);
return $this->db->affected_rows() > 0 ? $this->db->insert_id() : FALSE;
}
视图(book.php(
<div class="form-group form-float">
<p>Book Title</b></p>
<div class="form-line">
<input type="text" name="book_title" class="form-control" size="30" required>
</div>
</div>
<div class="form-group form-float">
<p>Book Category</b></p>
<div class="form-line">
<input type="text" name="book_category" class="form-control" size="30" required>
</div>
</div>
**//I got error on this code
<div class="body">
Book Title : <?php echo $book_title;?>
Book Category : <?php echo $book_category;?>
</div>**
我认为您加载了错误的视图:
$this->load->view('index', $page_data);
您应该调用正确的视图,然后才能正确地输出$book_title
&CCD_ 2。试试这个(考虑到这本书是你的正确观点(它可能会纠正你的错误:
$this->load->view('book', $page_data);
请参阅本用户指南,了解有关CodeIgniter 中视图的更多信息
试试这个
public function book_controller(){
$login_type = $this->session->userdata('login_type');
$page_data['login_type'] = $login_type;
$page_data['page_name'] = 'book';
$page_data['page_title'] = 'Book';
$page_data['page_function'] = __FUNCTION__;
$page_data['page_breadcrumb'] = generate_breadcrumb(array('Book'));
$page_data['book_title'] = $this->input->post('book_title');
$page_data['book_category'] = $this->input->post('book_category');
$this->md_book->addBook();
$this->load->view('index', $page_data);
}
将模型编辑为
function addBook(){
$data = array('book_title'=>$this->input->post('book_title'),'book_category'=>$this->input->post('book_category'))
$this->db->insert('tb_books', $data);
return $this->db->affected_rows() > 0 ? $this->db->insert_id() : FALSE;
}