Welcome Guest, Not a member yet? Register   Sign In
Use a model in view without controller
#1

[eluser]Unknown[/eluser]
I'm currently looking for a solution for the following problem.

I generate a custom menu in one of my views from my database. The MySQL query is in the faq_model. I already autoload my faq_model from the autoload.php configuration file to prevent the need to load it in every controller. The view is loaded in every controller as:

Code:
$data['faqcategories'] = $this->faq_model->getcategories();
$this->load->view('templates/metro/navbar',$data);

In the view I use a for loop to use this results.

The problem is that I currently have to use the line:
Code:
$data['faqcategories'] = $this->faq_model->getcategories();
in every single function in every controller and I want to get rid of that.

What is the best practice in a case like this? This is my first MVC project and I want to optimize my code.

Thank you in advance.
#2

[eluser]sv3tli0[/eluser]
I think its possible to call model with something as
Code:
$ci =& get_instance();
$ci->load->model('modelname');
$ci->modelname->function();

How ever you will lose the idea of the MVC if your view start makes decision instead the Controller...
Its easier to pass the model from your controller to the view..

Code:
$data['model'] = $this->modelname;
$this->load->view('template', $data);




Theme © iAndrew 2016 - Forum software by © MyBB