[eluser]theprodigy[/eluser]
I normally use MY_Controller to get data that will be used on every page (page specific title, meta tags, javascript, css, etc). In the MY_Controller, I just set variables for what they will be (or arrays in the case of javascript and css), and pass these variables to the view to use (depending on site, I sometimes just set css and js in controller methods rather than MY_Controller). MY_Controller extends Controller, and all my controllers extend MY_Controller
Controller:
Code:
$this->get_page_info(1); // number passed in is page id in database
MY_Controller:
Code:
public function get_page_info($page_id)
{
$this->load->model('page_model','page');
$page_data = $this->page->get_data($page_id);
$this->data['title'] = $page_data->title;
$this->data['meta_desc'] = $page_data->meta_desc;
$this->data['meta_keywords'] = $page_data->meta_keywords;
$this->load->vars($this->data);
$this->load->view('layout');
}
Page_model:
Code:
public function get_data($page_id)
{
$this->db->select('title, meta_desc, meta_keywords[, any other data you want]');
$this->db->from('pages');
$this->db->where('id', $page_id);
$page = $this->db->get();
if($page->num_rows() > 0)
{
return $page->row(); //you are getting data by primary key, assume only one row returned
}
else
{
return false;
}
}
Layout.php
Code:
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $meta_desc; ?>">
<meta name="keywords" content="<?php echo $meta_keywords; ?>">
</head>
<body>
/*
content goes here. Data array was loaded rather then passed in, so you can call other views from here and variables will be available inside them as well. Which is the reason I use a layout.php for my basic setup, then call in page specific views from there. Makes it easier that way.
*/
</body>
</html>