Pavel
Pavel

Reputation: 5353

How to invoke method from codeigniter controller when the page loads?

Can someone please tell me how to invoke a method from the codeigniter controller when the page loads? What I want to do is to fetch some data from database and put it inside meta tags before page actually load. I obviously want to do it inside header. It should be something like this:

method, which fetches some data from db and returns it
<meta property="og:title" content="<?php echo $returnedValue; ?>"/>

Can someone give me a hand with this one? Thanks.

Upvotes: 0

Views: 1114

Answers (1)

Tilman Koester
Tilman Koester

Reputation: 1739

The page only loads after your controller loaded the view. So run your method before you call the view and add the result to the $data array, then load the view with the $data array as a parameter.

  public function index()
  {
    $data['returnedValue'] = yourMethod();
    $this->load->view('template', $data);
  }

Now $returnedValue, in the view, holds the return value of your method. echo it in the view wherever you want to include it.

Upvotes: 3

Related Questions