Professional CodeIgniter

(singke) #1

Chapter 1: Welcome to the MVC World


17


Notice the use of the $content array whenever you access what you need. You may be wondering
about that. Take a look at the model again:

if ($q- > num_rows() > 0){
$data = $q- > row_array();
}

When the fetchHomePage() function retrieves the home page content from the pages table, notice the
use of row_array() , which converts the results into an array. The keys of that array are the field names,
with field values populating the array values. In other words, the row_array() method is a simple way
of keeping parity between your database tables and result set objects.


If you were to inspect the $data array within the model with print_r() , you ’ d probably see something
like this:


Array
(
[id] = > 4
[title] = > test
[keywords] = > test
[description] = > test
[status] = > live
[bodycopy] = > test
[css] = > default.css
)

Once called from the Page controller, however, this array is dropped inside $data[ ‘ content ’ ] , which,
when passed to the view, is accessible via the $content array. Why? Because you told the template to
accept the $data array when you loaded the view:

function index(){
$this- > load- > model(‘Page_model’,’’,TRUE);
$data[‘content’] = $this- > Page_model- > fetchHomePage();
$this- > load- > view(‘home’, $data );
}

Once the view is loaded, it unwraps whatever is in $data (itself an array), and the first thing it
encounters is the $content array embedded inside it. You see this kind of approach taken throughout
the book, so it becomes second nature to you.


Figure 1 - 5 illustrates the application flow.
Free download pdf