Professional CodeIgniter

(singke) #1

Chapter 1: Welcome to the MVC World


21


< ?php
class Page_model extends Model{

function Page_model(){
parent::Model();
}

function fetchHomePage(){
$data = array();
$options = array(‘status’ = > ‘live’, ‘type’= > ‘home’);
$q = $this- > db- > getwhere(‘pages’, $options, 1);

if ($q- > num_rows() > 0){
$row = $q- > row();
$data[‘title’] = $row- > title;
$data[‘css’] = $row- > css;
$data[‘keywords’] = $row- > keywords;
$data[‘description’] = $row- > description;
$data[‘bodycopy’] = nl2br($row- > bodycopy);

}
return $data;
$q- > free_result();
}
}
? >

In a lot of ways, this is a more palatable solution, because now the model does all the work of preparing the
data for display. However, others might argue that there may be a need in the future to have access to
the raw bodycopy field unprocessed by nl2br(). In that case, you ’ d want to keep the processing in the
controller or create a second method in the model to address this issue.

There is a much better way to do all this, of course, and it involves using a built - in CodeIgniter helper
called Typography. This helper has an auto_typography function that does all the hard work of
converting line breaks to < br/ > tags and converting quotes and dashes to the appropriate entities.


First, load the helper in the controller function:

function index(){
$data = array();
$this- > load- > library(‘parser’);
$this- > load- > model(‘Page_model’,’’,TRUE);
$data = $this- > Page_model- > fetchHomePage();

$this- > load- > helper(‘typography’);

$this- > parser- > parse(‘home’,$data);
}
Free download pdf