I have following model and controller. And database table pages has id, title, content and slug.
Q1. Why does the line in controller,
$this->load->model("pagemodel", 'pages'); // Load the page model,
have 'pages'?
Is it naming "pagemodel" as pages?
And I see a line,
$page_data = $this->pages->fetch($page_slug); // Pull the page data from the database
using pages.
Model code
<?php
class Pagemodel extends Model
{
/**
* Constructor
*/
function Pagemodel()
{
parent::Model();
}
/**
* Return an array of pages — used in the manage view
*
* @access public
* @return array
*/
function pages()
{
$query = $this->db->query("SELECT * FROM `pages` ORDER BY `id` ASC");
return $query->result_array();
}
/**
* Return a list of a single page — used when editing a page
*
* @access public
* @param integer
* @return array
*/
function page($id)
{
$query = $this->db->query("SELECT * FROM `pages` WHERE `id` = '$id' LIMIT 1");
return $query->result_array();
}
/**
* Return an array of a page — used in the front end
*
* @access public
* @param string
* @return array
*/
function fetch($slug)
{
$query = $this->db->query("SELECT * FROM `pages` WHERE `slug` = '$slug'");
return $query->result_array();
}
/**
* Add a record to the database
*
* @access public
* @param array
*/
function add($data)
{
$this->db->query("INSERT INTO `pages` (`title`, `content`, `slug`) VALUES (".$this->db->$data['title'].", ".$this->db->$data['content'].", ".$this->db->escape($data['slug']).")");
}
/**
* Update a record in the database
*
* @access public
* @param integer
* @param array
*/
function edit($id, $data)
{
$this->db->query("UPDATE `pages` SET `title` = ".$this->db->escape($data['title']).", `content` = ".$this->db->escape($data['content']).", `slug` = ".$this->db->escape($data['slug'])." WHERE `id` = '$id'");
}
/**
* Remove a record from the database
*
* @access public
* @param integer
*/
function delete($id)
{
$this->db->query("DELETE FROM `pages` WHERE `id` = '$id'");
}
}
?>
Controller code
<?php
class Page extends Application
{
function Page()
{
parent::Application();
$this->load->model("pagemodel", 'pages'); // Load the page model
}
function _view($page, $page_data)
{
$meta = array (
'meta_title' => 'title here',
'meta_keywords' => 'keywords here',
'meta_description' => 'description here',
'meta_robots' => 'all',
'extra_headers' => '
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
'
);
$data = array();
// add any data
// merge meta and data
$data = array_merge($data,$meta);
// load view with data
$this->load->view('header', $data);
$this->load->view($page, $page_data);
$this->load->view('footer');
}
function index()
{
$page_slug = $this->uri->segment('2'); // Grab the URI segment
if($page_slug === FALSE)
{
$page_slug = 'home'; //Change home if you change the name in the back-end
}
$page_data = $this->pages->fetch($page_slug); // Pull the page data from the database
if($page_data === FALSE)
{
show_404(); // Show a 404 if no page exists
}
else
{
$this->_view('index', $page_data[0]);
}
}
}
?>