0

Possible Duplicate:
Call to a member function on a non-object

In my homepage view I've trying to pass data to my header sub-view

<?php
  $this->load->model('header2');
  $head = $this->header2->HeaderData();
  $this->load->view('head_view', $head);
?>    

but I get this error:

Message: Undefined property: CI_Loader::$header2
Fatal error: Call to a member function HeaderData() on a non-object in H:\Forum\application\views\homepage_view.php on line 6

Community
  • 1
  • 1
SteB
  • 1,999
  • 4
  • 32
  • 57
  • It seems that your `header2` model isn't working properly. Give the result of `var_dump($this->header2);`. – Repox Nov 25 '12 at 19:22
  • @Repox - var_dump returns "NULL", the HeaderData method works fine from a controller – SteB Nov 25 '12 at 20:12
  • I missed the part where you were trying to use loaded models in your views. You cant. The answer below is the correct way to do it. – Repox Nov 25 '12 at 20:18
  • @hakre - How do I load a model inside a view to pass it's data to a sub-view? – SteB Nov 25 '12 at 20:40
  • @SteB: What is `var_dump($this->header2)`? It does not look like it is an object and also it does not have the `HeaderData()` method. Are you sure the viewtemplate in codeigniter framework allow you to load models? I'm not so fluent with CI, so just asking, because if CI does not support model loading in views, the code-example you've put in your question would not make much sense. – hakre Nov 26 '12 at 09:22

1 Answers1

1

write your code in controller method and load your view form there only, and use code like this

 $this->load->model('header2');
 $data['head'] = $this->header2->HeaderData();
 $this->load->view('head_view', $head);

and in the head_view.php view access this property as $head like this

echo '<pre>';
print_r($head);
echo '</pre>';

if you are loading main view and then calling subview in it then you don't need to pass a value while loading view and you can directly access value in subview, so you can directly use $head in subivew.

Pankaj Khairnar
  • 3,028
  • 3
  • 25
  • 34
  • So if any data I pass to the homepage view will be available in sub-views, I just need to pass data from 2 models to the homepage view? – SteB Nov 25 '12 at 20:38
  • yes, you just need to pass a data to main view in key=>value pair, your keys will be variable in main view like you can use $key, and if you are loading subview from main view you don't to pass data/key=>value pair to subivew $key will be available in subview – Pankaj Khairnar Nov 26 '12 at 02:08