Welcome Guest, Not a member yet? Register   Sign In
Req: Idea to avoid lots of view files
#1

[eluser]CARP[/eluser]
Hi
I always start a CI application doing lot of CRUD.
I've already a base set of files, but want to avoid creating lots of views, for example:
- category_add
- category_edit
- category_list
- category_post_add
- category_post_edit

How could I remove category_post_add view (which tells user object was added correctly) and category_post_edit (which tells user object was modified correctly)

1) How could I create a global "object_post_add" view and use in every object's crud?
2) How can I make a link in that global view so when user clicks it goes back to where it came from? - and not by doing [removed]history.go(-1)

Hope you've understood my idea, and I'm open to any suggestion to improve this.
Thanks!!
#2

[eluser]mddd[/eluser]
I don't see why you would make a seperate "item added" view. This is only annoying to the users, as this page would have no real information on it. Only saying 'it was added, click to continue' ? That makes no sense.

I think it would be better just to go directly to the list view, and displaying a line there saying 'item X was added'. This is super easy, just something like this in the view file:
Code:
<?php if ($item_added) { ?>
   <p>The item &lt;?$item_added?&gt; was added to the list.</p>
&lt;?php } ?&gt;
of course, you need to pass the name of the item that was added to the view along with the contents of the list you want to display.

I don't think you would need to make this a separate view file, but if you wanted to, you could of course do that. And then just call it from the list view using
Code:
$this->load->view('item_added', array('item_added'=>$item_added'));
or something like that.

To your second question, if you name your controllers logically, for instance /category/add then you just have to go back to /category.
So you would do something like
Code:
<a >uri->segment(1))?&gt;">Back to the categories</a>
because $this->uri->segment(1) will contain the name of your controller (if you haven't done re-routing).
#3

[eluser]erik.brannstrom[/eluser]
I agree with mddd, loading separate views just to say everything is nice and dandy is pretty useless. Check out Sessions in the user guide (http://ellislab.com/codeigniter/user-gui...sions.html) and especially the part about flashdata. That's the way I'd do it anyway.

Also, you could probably reuse the same view for both editing and creating since these two usually look the same. Just use the same view and send a variable telling the view whether or not it is an edit. If it is, fill out the values else keep them empty.

EDIT: Also, regarding going back in history, I can recommend the History library by Peter Goodman (http://ellislab.com/forums/viewthread/58091/). Haven't used it for a while though, but as far as I know it should still work.
#4

[eluser]Zack Kitzmiller[/eluser]
I always redirect to the index page, and display a message.

Flashdata is the way to go.
#5

[eluser]CARP[/eluser]
Wow, thanks guys for the suggestions...

EDIT:

1. In my controller, I have a delete function. Once the deletion is made, I do:
Code:
...
$this->session->set_flashdata('msg', 'Category deleted!');
$this->list_categories();

2. When the category_list loads again, the flashdata session is not shown, but When I press F5 in browser, it shows fine.

I guess that the problem must be the call to list_categories() function from within the delete() function in my controller. That must not make a new server request, right? How could I fix this?

EDIT2: SOLVED AND WORKING !!

I must use
Code:
redirect('category/list_categories','refresh');

instead of
Code:
$this->list_categories();

Thanks!

 




Theme © iAndrew 2016 - Forum software by © MyBB