我有以下controllers, models & views
结构。
class Content extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index() {
$this->load->view("global_header");
$this->load->model("ContentModel");
$data['result'] = $this->ContentModel->blogPostContents();
$this->load->view("content_page", $data);
$this->load->view("global_footer");
}
}
class ContentModel extends CI_Model {
public function blogPostContents() {
$url_segment = $this->uri->segment(1); // Get post title
$this->db->select('*');
$this->db->from('blog_posts');
$this->db->where("url", $url_segment);
$query = $this->db->get();
$query->result();
$this->db->last_query();
return $query->result();
}
}
<article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<?php
echo $result[0]->post;
?>
</div>
</div>
</div>
</article>
现在我在控制器文件content.php
中创建了另一个函数:
public function show() {
$data['title'] = "Hello";
$data['body'] = "World";
$this->load->view('content_page', $data);
}
现在,当我试图在content_page文件中回显$title & $body
变量时,我变量是未定义的。任何想法我做错了什么?
错误列表:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: title
Filename: views/content_page.php
Line Number: 5
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: body
Filename: views/content_page.php
Line Number: 6
所以我想知道如何显示要查看的变量值。
感谢。
答案 0 :(得分:0)
在控制器索引()中尝试添加以下行:
class Content extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index() {
$this->load->view("global_header");
$this->load->model("ContentModel");
$data['result'] = $this->ContentModel->blogPostContents();
//$this->load->view("content_page", $data);
$this->show($data);
$this->load->view("global_footer");
}
}
在show方法或功能中
public function show($data) {
$data['title'] = "Hello";
$data['body'] = "World";
$this->load->view('content_page', $data);
}
希望它运作良好