My 'Display' Model

As promised (and slightly simplified) here is my display model:

<?php class Display extends Model {

/*create the array to pass to the views*/

var $data = array(); /*two other class variables*/ var $base; var $status = '';

/*the constructor function: this calls the 'model' parent class, loads other CI libraries and helpers it requires, and dynamically sets variables*/

function Display() {

/*now set the standard parts of the array*/

$this->data['css'] = $this->config->item('css'); $this->data['base'] = $this->config->item('base_url'); $this->base = $this->config->item('base_url');

$this->data['myrobots'] = '<meta name="robots"

content="noindex,nofollow">'; /*note that CI's session stuff doesn't automatically recall the extra variables you have added, so you have to look up the user's status in the ci_sessions table*/

$sessionid = $this->session->userdata('session_id'); $this->db->select('status'); $this->db->where('session_id', $sessionid); $query = $this->db->get('ci_sessions');

/*function to assemble a standard page. Any controller can call this. Just supply as $mydata an array, of key/value pairs for the contents you want the view to display. Available variables in this view are: mytitle. menu, mytext, diagnostic */

function mainpage($mydata) {

$this->data['mytitle'] = 'Monitoring website'; $this->data['diagnostic'] = $diagnostic; foreach($mydata as $key => $variable) {$this->data[$key] = $variable;} /*here's the menu class we looked at in Chapter 3*/ $fred = new menu; $this->load->library('session');

$mysess = $this->session->userdata('session_id'); if(isset($this->status) && $this->status > 0) {$this->data['menu']=

$this->load->view('basic_view', $this->data); }

I can call the main page from any controller with the lines:

$this->load->model('display'); $this->display->mainpage($data);

and I then know that my view is being assembled dynamically, exactly as I want it.

Was this article helpful?

0 0

Post a comment