CodeIgniter Forums
How to call controller from view without form submission ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: How to call controller from view without form submission ? (/showthread.php?tid=68810)

Pages: 1 2


How to call controller from view without form submission ? - ktmonty - 08-29-2017

How to call controller from view without form submission ?


RE: How to call controller from view without form submission ? - InsiteFX - 08-29-2017

Using Ajax


RE: How to call controller from view without form submission ? - ktmonty - 08-31-2017

(08-29-2017, 10:01 AM)InsiteFX Wrote: Using Ajax

Thanks InsiteFX,
   i am in situation where ajax is not option. I am in a loop where i get menu id and i want to fatch the submenu from that id.


RE: How to call controller from view without form submission ? - Kaosweaver - 09-01-2017

What you really need is a model to load in your view so your menu can read the submenu items.
To do this you could just pull in the instance of CI, then call your model, then call the subquery:
PHP Code:
$this->_ci = &get_instance();
$this->_ci->load->model('Menu_model');

// inside your loop
$this->_ci->Menu_model->get_submenu($row->top_menu_id); 
Obviously, using your own model name, column names and function calls (since I didn't know them, I made mine up)


RE: How to call controller from view without form submission ? - rtenny - 09-01-2017

Even tho this will work ist not a good pratice to get the data in the view file. Strictly speaking the view creates the html and should have all the data needed to do this.

I would get the data in the controller and pass it to the view. In the controller you very liklely have already loaded the model so why would you load it again in the view.

But this is just my personal preference.


RE: How to call controller from view without form submission ? - Kaosweaver - 09-01-2017

Since this is a menu (from what I gather), if I were to put it in the controller (which I agree is where it belongs, strictly speaking) - I would create a MY_Controller, put it there and then have all of the other controllers inherit it. This way, the menu system isn't being put everywhere and you've got one isolated location for it.
The code to create all of the data (as I'm assuming dynamic menus that change here) is also a little annoying to do without incorporating some view stuff in the controller, you have to create the loop, then get the view for the menu and its sub items, then merge them all, then pass to the final view. Seemed like a lot of extra work for the exact same result with no real life benefit, which is why I recommended what I did.


RE: How to call controller from view without form submission ? - ktmonty - 09-04-2017

(09-01-2017, 06:14 AM)Kaosweaver Wrote: What you really need is a model to load in your view so your menu can read the submenu items.
To do this you could just pull in the instance of CI, then call your model, then call the subquery:
PHP Code:
$this->_ci = &get_instance();
$this->_ci->load->model('Menu_model');

// inside your loop
$this->_ci->Menu_model->get_submenu($row->top_menu_id); 
Obviously, using your own model name, column names and function calls (since I didn't know them, I made mine up)

Hi Kaosweaver,
 
Thanks for your reply with code it helps. Is there any way to do like i have my leftsidebar_contolle,leftsidebar _model and leftsidebar _view so i can load them once and use in application. like other controller,model,views we use.


RE: How to call controller from view without form submission ? - ktmonty - 09-04-2017

(09-01-2017, 08:12 AM)Kaosweaver Wrote: Since this is a menu (from what I gather), if I were to put it in the controller (which I agree is where it belongs, strictly speaking) - I would create a MY_Controller, put it there and then have all of the other controllers inherit it.  This way, the menu system isn't being put everywhere and you've got one isolated location for it.
The code to create all of the data (as I'm assuming dynamic menus that change here) is also a little annoying to do without incorporating some view stuff in the controller, you have to create the loop, then get the view for the menu and its sub items, then merge them all, then pass to the final view.  Seemed like a lot of extra work for the exact same result with no real life benefit, which is why I recommended what I did.


Thanks for reply. Is there any way to do like i have my leftsidebar_contolle,leftsidebar _model and leftsidebar _view so i can load them once and use in application. like other controller,model,views we use.


RE: How to call controller from view without form submission ? - jessicab - 09-22-2017

I think Ajax can be a good option though.


RE: How to call controller from view without form submission ? - skunkbad - 09-22-2017

Whatever you're doing, you're doing it wrong.