Welcome Guest, Not a member yet? Register   Sign In
How to call controller from view without form submission ?
#1

How to call controller from view without form submission ?
Reply
#2

Using Ajax
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 08-31-2017, 10:43 PM by ktmonty.)

(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.
Reply
#4

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)
Reply
#5

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.
On the package it said needs Windows 7 or better. So I installed Linux.
Reply
#6

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.
Reply
#7

(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.
Reply
#8

(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.
Reply
#9

I think Ajax can be a good option though.
Reply
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB