Chapter 8: Last-Minute Upgrades
238
Creating the Model
The model for the Pages table should be kept to a bare minimum at this point. All you need are functions
for retrieving one or all pages, for deleting a page, and for adding and updating a page.
As usual, start the model in the classic way. Note that following the earlier pattern, you ’ ll call this model
MPages :
< ?php
class MPages extends Model{
function MPages(){
parent::Model();
}
The getPage() function pulls out one page from the Pages table, based on ID.
function getPage($id){
$data = array();
$this- > db- > where(‘id’,$id);
$this- > db- > limit(1);
$Q = $this- > db- > get(‘pages’);
if ($Q- > num_rows() > 0){
$data = $Q- > row_array();
}
$Q- > free_result();
return $data;
}
The getPagePath() function does the same thing as getPage() , except you pass in a path as an
argument. You ’ ll be using this function on the Welcome controller to extract one page for a given address.
function getPagePath($path){
$data = array();
$this- > db- > where(‘path’,$path);
$this- > db- > where(‘status’, ‘active’);
$this- > db- > limit(1);
$Q = $this- > db- > get(‘pages’);
if ($Q- > num_rows() > 0){
$data = $Q- > row_array();
}
$Q- > free_result();
return $data;
}