I’m starting to learn how to use CodeIgniter, and I want to create static pages using database.
My View:
<html>
<head>
<title>{blog_title}</title>
</head>
<body>
<h1>{page_title}</h1>
<p>{page_content}</p>
</body>
</html>
My Controller:
<?php
class Pages extends CI_Controller {
public function view($page = 'contact') {
$this->load->model('Model_Pages');
$data['data'] = $this->Model_Pages->get_data_page($page);
$this->load->view('template/header');
$this->load->view('pages/' . $page, $data);
$this->load->view('template/footer');
}
}
My Model:
<?php
class Model_Pages extends CI_Model {
public function get_data_page($slug) {
$this->db->select('blog_title', 'page_title', 'page_content');
$this->db->from('pages');
$this->db->where('slug', $slug);
$q = $this->db->get();
return $q->result();
}
}
Why it does not display the database data?
Instead of
result()userow()since you only expect/want one row:result()returns an array of results, even if there is only one. It would be a good idea to validate that you got a result as well.Next, instead of
$data['data'], just use$dataIn fact, you might need
row_array()instead in case CI’s (very limited) template parser doesn’t work with object data types:This should create a key/value array structure that CI’s parser can use.