Professional CodeIgniter

(singke) #1

Chapter 4: Creating the Main Web Site


80


foreach ($Q- > result_array() as $row){
$data[$row[‘id’]] = $row[‘name’];
}
}
$Q- > free_result();
return $data;
}

As stated in Chapter 3 , you can also get the result rows as objects by using the arrow syntax:

function getCategoriesNav(){
$data = array();
$Q = $this- > db- > get(‘categories’);
if ($Q- > num_rows() > 0){
foreach ($Q- > result() as $row){
$data[$row- > id] = $row- > name];
}
}
$Q- > free_result();
return $data;
}

From a performance standpoint, it ’ s tomato – tomahto.

Now call this new function instead of getAllCategories() in your controller function:

function index(){
$data[‘title’] = “Welcome to Claudia’s Kids”;
$data[‘navlist’] = $this- > MCats- > getCategoriesNav();
$this- > load- > vars($data);
$this- > load- > view(‘template’);
}

When you reload your browser, you should still see two errors, as shown in Figure 4 - 2.

Figure 4 - 2
Free download pdf