Welcome Guest, Not a member yet? Register   Sign In
How to use variables from class globally in views
#4

[eluser]laytone[/eluser]
Fast Answer:

Create a class called MY_Controller in your libraries folder

and create a function that retrieves your message count so like this:

Code:
class MY_Controller extends Controller {
  function MY_Controller(){
    parent::Controller();
  }

  function _get_mailbox_count($mailbox){
    $this->load->model('member/Messages_model');
    return $this->Messages_model->get_folder_count($mailbox);
  }
}

Now your controller:

Code:
class Messages extends MY_Controller {
  function Message(){
   parent::MY_Controller();
  }

  function Anyfunction(){
    $inbox_count = $this->_get_mailbox_count('INBOX');
    $sentbox_count = $this->_get_mailbox_count('SENT');
    //etc...
  }
}

Doing this will solve your problem.


This is how I set up my controllers. Notice I crate a _showPage function to load my template and views, this way I can make sure I can send any variables I want to my pages plus I can make sure views get loaded with a set of pre-defined variable plus the new ones I send to it. _showPage is always avail in any controller as long as my controller extends MY_Controller and not Controller

First I extend the controller like this:

Code:
class MY_Controller extends Controller {
    
    var $current_controller;
    var $current_method;
    var $default_image_folder = '';
    var $default_css_folder = '/css/';
    
    public function __construct(){
        parent::Controller();
        $this->current_controller = $this->uri->segment(1);
        $this->current_method = $this->uri->segment(2);
//I get my controller name and that is the same as the 'view' folder
    }
    
    function _showpage($pagename = '', $pagedata = array(), $pagetitle = "Default Page Title", $controller = ''){
        $controller = ($controller == '') ? $this->current_controller : $controller;
        
        $pagedata['page_title'] = 'Sitename - '.$pagetitle;
        $pagedata['css_folder'] = $this->default_css_folder;
        $pagedata['images_folder'] = $this->default_image_folder;
        $pagedata['message'] = (!isset($pagedata['message'])) ? $this->session->flashdata('message') : $pagedata['message'];
        $pagedata['page_body'] = $this->load->view($controller.'/'.$pagename, $pagedata, TRUE);
        $pagedata['top_menu'] = $this->load->view('template/top_nav', $pagedata, TRUE);
        $pagedata['side_bar'] = $this->load->view($controller.'/side_bar', $pagedata, TRUE);
        $this->load->view('template/default.php', $pagedata);
    }
}

Use the constructor to load the values that you need between all of your pages. Then add values to the $pagedata array (before you load the views for nave and body). Like this:

Code:
...
function __construct(){
  $this->load->model('Message_model');
  $this->inbox_count = $this->Message_model->get_message_count('inbox');
}
function _showPage(...){
   ...
   //I would add a line like this:
   $pagedata['inboxcnt'] = $this->inbox_count();
}

Now your regular controllers just extend MY_Controller instead of Controller and you call the _showPage() function to load you views and you global variables are always available to you in any you views.

This method words great for templates and stuff

Hope I helped, This is the best way to do this (even if you don't use a _pageload function like I like to.... Like the guy said before me.. extend your controller and load the variables with a constructor.

Layton


Messages In This Thread
How to use variables from class globally in views - by El Forum - 01-10-2010, 09:02 PM
How to use variables from class globally in views - by El Forum - 01-10-2010, 09:15 PM
How to use variables from class globally in views - by El Forum - 01-10-2010, 09:24 PM
How to use variables from class globally in views - by El Forum - 01-11-2010, 01:38 AM
How to use variables from class globally in views - by El Forum - 01-11-2010, 11:10 AM
How to use variables from class globally in views - by El Forum - 01-11-2010, 05:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB