0

Task

Grab user's balance from Database on every new page load. Cannot store in session as there is a bonus system which can add to user's balance while he is logged in.

In the way my page is set up it has 4 parts:

$this->load->view('head'); //Head, meta, css, js
$this->load->view('navbar'); //Strip 50px height at top of page. Has navigation, logout and user balance
$this->load->view('xxx'); //Unique page content
$this->load->view('footer'); //standard footer

I can pass information to the view through the additional parameter:

$this->load->view('navbar', $balance);

However, that means everytime the page is called, I have to call the database from the controller somewhere.

What I am looking for is an idea of how to realize so everytime when I call the navbar view, it automatically calls a controller function which returns user balance.

I imagine something like this:

$this->load->view('navbar', function getBalance() {
   return callControllerAndGetBalance();
});

Had a dirty idea of putting an AJAX call in the navbar which on page load calls the controller and then changes the value, but that just seems wrong.

Any ideas?

1
  • Off off topic all new user guides for codeigniter 2 and codeigniter 3 now at codeigniter.com/docs Commented Apr 23, 2015 at 7:06

1 Answer 1

2

You can use CodeIgniter hooks:

https://ellislab.com/codeigniter/user-guide/general/hooks.html

Untested, but this is how I think it must work. Taking this from that quora link I put in the comments, it looks like when you call $controller =& get_instance(); then that is the controller and you can add a property to it and it will be there for your view.

I'd play with the different options as well, this could be promising depending on what you are doing in your __construct method:

post_controller_constructor Called immediately after your controller is instantiated, but prior to any method calls happening.

class Blog extends CI_Controller{    

       public $balance = 0;

       public function getBalance(){        
             $this->balance = Balance::getMe();   
       }
}

What you can do is call the get_instance helper function from your hook.

class PostControllerHook{  
      function post_controller($params)  {     
          // $params[0] = 'controller' (given the params in the question)     
          // $controller is now your controller instance,     
          // the same instance that just handled the request     
          $controller =& get_instance();     
          $controller->getBalance();  
      }
}

then use

$this->load->view('navbar', $this->balance);
Sign up to request clarification or add additional context in comments.

4 Comments

I am afraid to ask which hook to use. I sense post_controller could be the one, but I can't really think of how to intergate it.
I haven't actually done this, though I've used CodeIgniter a lot. I'd guess the same actually - post_controller. The reason being, you're doing a pretty simple operation thats just an add on. Here's a little example: quora.com/Hooks-concept-in-codeigniter-with-simple-examples
My uncertainty of how to use it still stands. I don't know how to pass the data to the view.
This is not exactly it, because then each controller has to be extended this way, as I have multiple controllers. I decided to use Ajax and just reset the balance. The speed does not matter as much. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.