Welcome Guest, Not a member yet? Register   Sign In
Multiple functions for a single view. Need help!
#1

[eluser]Publisher1[/eluser]
I sometimes have trouble with codeigniter, here I am again. Can you maybe give me tips?

I have a View that would show my 'products’ in my project. Initially I had the pages for the inserting, editing and deletion of the products into separate controllers, models and views. The controller logics then each was all just in the index () method.

Some big methods I needed in all models. To not copy paste large code several times I got the idea for this functionality to move all in one controller, model and view. So I had made ​​methods for inserting, editing and deletion.

But now I do not know how to do that in the view. How should I tell the view which content for example on products/insert he has to show only the datas from the method insert()? and products/delete only the datas of the metod delete() etc? How can I implement all this in a view?

Or do you suggest me something else?

Thank you in advice!
#2

[eluser]ortwin.van.vessem[/eluser]
Hi Pub1,

If I understand correct you want one single view for multiple actions (add, edit and delete). It is possible to do this with one single view although I do not advise this approach. It is much cleaner to create separate views for various actions.

To do it with one single view file you need to determine which action you are performing. Easiest way to check that is via the URI-segment. For example

Code:
http://mydomain.tld/customers/add/

Code:
http://mydomain.tld/customers/edit/1

Code:
http://mydomain.tld/customers/delete/1

In your view file add the following:

Code:
<?php if (uri->segment(2) == "edit") : ?>

form for adding a customer

<?php elseif (uri->segment(2) == "delete") : ?>

confirmation form for deleting a customer

<?php else : ?>

form for adding a customer

<?php endif; ?>
#3

[eluser]AlexandrosG[/eluser]
My friend, the fact that you need some methods to be available in all models is not a reason to have only one model or controller or view. For example, if you want a method to be available in every model, you can extend the CI_Model class.

Having only one view for many "pages" leads you to put business logic inside your views and destroys your MVC architecture. Furthermore, i don't undestand why you need many models (or controllers) to implement CRUD for a single table (products). I would say that you can have one controller and one model for operations that relate to certain table, and a separate view for each "page". This is not an absolute rule, but something you can rely on for the start.
#4

[eluser]Publisher1[/eluser]
Hello Everybody

Thank you very much, now i have 1 controller, 1 model, and multiple views which are in a subfolder called like the Controller/Model. I've Now solved so. Had initially wrongly so in my head that one per controller must also have 1 model and 1 view strictly.

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB