I have following controllers, models & views
structure.
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>
Now I have created another function in my controller file content.php
:
public function show() {
$data['title'] = "Hello";
$data['body'] = "World";
$this->load->view('content_page', $data);
}
Now when I am trying to echo $title & $body
variable inside content_page file I am getting variable is undefined. Any Idea what I am doing wrong?
Error List:
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
So I want to know how to display variable values to view.
Thanks.
In controller index () try adding this line:
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");
}
}
In show method or function
public function show($data) {
$data['title'] = "Hello";
$data['body'] = "World";
$this->load->view('content_page', $data);
}
Hope it works fine