0

I just started to use codeigniter. I need to know the explanation of Views are never called directly.

Is it mean that i can not use $this->load->view('My_view') into another view?

I created a project in core php and decided to convert into codeigniter. In my project a page has different section so i created a main file recipes.php in views. I also created a folder where i put different section files to include in recipes.php. In my controller i loaded recipe view, it showed the recipe page then inside recipes view i used $this->load->('categorymenu'). It worked fine. I didnt pass any data since this section contains simple html. But im confused that it is not a right way of loading views.

So please can someone explain in detail. Am i doing the right thing or is there another way of doing it.

I also loaded the view in controller and passed to main view in controller which also worked perfect. But as i mentioned im not sure which is the right approach. My apology if this is a stupid question.

  • 1
    Possible duplicate of [Best method of including views within views in CodeIgniter](http://stackoverflow.com/questions/15221371/best-method-of-including-views-within-views-in-codeigniter) – RïshïKêsh Kümar Apr 27 '17 at 17:45
  • http://stackoverflow.com/questions/15221371/best-method-of-including-views-within-views-in-codeigniter -- Check it Out This... – RïshïKêsh Kümar Apr 27 '17 at 17:46
  • I do what you did in your first attempt, works fine all the time. But your second approach and what is mentioned as the best approach in the asnwer suggested by @RïshïKêshKümar seems like the correct thing to do, that way you do not have to dig up view by view to make changes, everything right in the controller. – coderodour Apr 27 '17 at 17:48
  • Actually i did check the response by @RïshïKêshKümar and used the code. It helped me a lot. I was confused about right one. – Fayjovi Apr 27 '17 at 18:36
  • @Fayjovi .. Thanks U !! Bro – RïshïKêsh Kümar Apr 27 '17 at 18:49
  • Thank you too @RïshïKêshKümar – Fayjovi Apr 27 '17 at 18:54

1 Answers1

0

As you are naive to Codeigniter no question asked by you is stupid. Now the answer of your question is that,

  1. You can load a view inside another view and it is absolutely acceptable as there will be some cases when you have to put header in all pages of the web site, that time you have to load the view in another view.
  2. Second is that the correct way to load the view is in the controller. As the controller is the mediator between the model and view. if you want some data of database to be displayed in your page you have to get that data through the function created in the model and then after you can load the view in controller and can pass the data in that.

This is code of controller

public function temp()
{
   $data['recs']=$this->my_model->my_function(); //getting data to pass in view
   $this->load->view('my_view',$data); //load the view
}
  • Thanks for your answer. So it means if im not passing data then i can load view inside other view for example simple html and for database specific i should use controller. – Fayjovi Apr 27 '17 at 18:34
  • It took me two days and finally got the answer. Again thanks for your help. – Fayjovi Apr 27 '17 at 18:51