views:

561

answers:

3

can anyone tell me how do i access model from view in codeigniter?

+1  A: 

See the thread:

View Calling a Model

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.

Sarfraz
nothings here notes that there is processing in the view. In MVC, the view has access to the model. See: http://www.phpwact.org/pattern/model_view_controller
Thorpe Obazee
ok then how do i access 3 tables using controller-model method? i had to select data from 3 tables and all of them depends in the value generated from 1st query, is there any other easy way, i was successful to create join in 2 tables but no idea in 3 tables joins, any suggestion?
sonill
A: 

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

naivists
+1  A: 

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?

bschaeffer