Welcome Guest, Not a member yet? Register   Sign In
running some function before view
#1

Hi, 


I put notification bar on topbar. the data for notifications are set in MY_Controller.php
problem is:

For example there is notification where it says user has N unread messages. 
where N set in MY_Controller.
 
and then when user goes to unreaded message page, that notification should be decreased by 1, 
but as N set in MY_Controller and thus runs before Message Controller runs it will not be decreased by one.

What is the good way to solve this?

Thanks
Reply
#2

It is nearly impossible to comment without seeing the code you written.
Reply
#3

(This post was last modified: 05-31-2017, 10:48 AM by PaulD.)

You are using the pre-controller functions to do something that should not be done in the pre-controller IMHO. Often when implementing a complex header and sidebar, I will create a navigation_model that returns all the required data for the sidebar and header. Called just before the page loads the views, it will always be up to date.

For instance, every controller has this just before the page views are called:

PHP Code:
// get navigation data
$header_data['navigation_info'] = $this->navigation_model->get_navigation_info($user_id);

// load page views
$this->load->view('header_view'$header_data);
$this->load->view('header_view'$page_data);
$this->load->view('footer_view'$footer_data); 

Then, if I alter the sidebar, I just need to alter the one library file to provide data for all the pages using that layout.

It is much easier in fact if you have a layout_model or template_model to load the views, such as:

PHP Code:
$this->layout_model->render_page('dashboard'$page_data); 

Not a complicated model, but just means that if I decide to split the header and sidebar views, and have a right sidebar and left sidebar, or whatever, I only have to change the layout model, which itself calls the navigation model to get sidebar data for.

Now your controller has just one line in it to output the page once all your actions have been performed and your page data gathered. All the template or theme or layout data is gathered in the layout model. This also has the benefit that you can completely change the layout and design of your site, without having to address every controller call to load views. You only have to update the one layout_model.
Reply
#4

Thank u PauldD!
 
It solves my issue! thanks a lot
Reply




Theme © iAndrew 2016 - Forum software by © MyBB