can anyone tell me how do i access model from view in codeigniter?
See the thread:
By the way why do you need to access the model
from the view
, you can send the model data to the view from the controller
too which is the usual and better approach.
As a good note, keep your processing logic out of the view, you should use controller
instead.
Ideollogically, what you are asking for, is wrong - model should be accessed from controller only. Controller uses the model to generate data and uses the view to display the data. Still, if you want to do this, follow the advice from @Sarfraz
CodeIgniter's $this->load->model()
returns absolutely nothing. Look at it: system/libraries/Loader.php
.
This will output absolutely nothing:
$model = $this->load->model('table');
print_r($model);
And this next example will give you the fatal error Call to a member function some_func() on a non-object
:
$model = $this->load->model('table');
$model->some_func();
It doesn't matter whether that function even exists, $model
is not an object.
The thing to do is have a method in your model that returns data, then call that function and pass the results to your view file:
$this->load->model('table');
$data = $this->table->some_func();
$this->load->view('view', $data);
PS: How is the only answer you've accepted the absolute wrong thing to do?