CodeIgniter Forums
Redirecting to the same page after edit - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Redirecting to the same page after edit (/showthread.php?tid=20071)



Redirecting to the same page after edit - El Forum - 06-26-2009

[eluser]quest13[/eluser]
Hi, Is there any way I can redirect to the specific page after edit or delete ?

Say I have 40 records and I have 8 records per page and If I edit 32 th record, after update, I want to redirect the user to 3 page.But It is redirected to first page always by default.


Redirecting to the same page after edit - El Forum - 06-27-2009

[eluser]tonanbarbarian[/eluser]
assuming you are using the pagination library... i have previously modified the pagination library to be able to retrieve the pagination data from the session
That way you can leave the page and come back at any time and it is still on the same page it was last time
i do the same with any search filters I might have on a list as well


Redirecting to the same page after edit - El Forum - 06-27-2009

[eluser]quest13[/eluser]
Thanks for the response. I am using the CI library only. However, I will try with the session and come back


Redirecting to the same page after edit - El Forum - 07-28-2009

[eluser]steve_mock[/eluser]
Related:

Jumping to the appropriate page/offset immediately after a new record is inserted?

Sounds hard. But, thanks in advance.


Redirecting to the same page after edit - El Forum - 07-28-2009

[eluser]Dam1an[/eluser]
[quote author="steve_mock" date="1248833546"]Related:

Jumping to the appropriate page/offset immediately after a new record is inserted?

Sounds hard. But, thanks in advance.[/quote]

It's not really that hard
Once you insert a new item, you need to find out the totaly number of rows (you already do this when creating the pagination links) and divide that by however many items/page. That gives you the oage number, so just redirect to the pagination URL with that number as the last segment... make sense?


Redirecting to the same page after edit - El Forum - 07-28-2009

[eluser]steve_mock[/eluser]
[quote author="Dam1an" date="1248834052"]... make sense?[/quote]

Perfectly. Thank you.

However... the freshly inserted record may not fall at the very end. Smile

I am sorting my tables by user-entered event date (not a timestamp) and events are created non-chronologically. So my "target" record is likely somewhere in the middle of the total count.

I reckon I could get its active record row number (after the sort) and do a little math to cough up the offset... hmmm.

-s


Redirecting to the same page after edit - El Forum - 07-28-2009

[eluser]Dam1an[/eluser]
Ah, you like to make things a little more interesting then
Here's a little example which should point you in the right direction, except I'll be using names instead of dates, but its basically the same thing

Code:
// Insert the user and get their ID
$this->db->insert('users', array('name'=>'Damian'));
$user_id = $this->db->insert_id();

// Get all the users sorted by name
$this->db->order_by('name', 'asc');
$query = $this->db->get('users');
$users = $query->result();

// Loop through the users till we find the one with the same ID
// Need the key as well as the value so we can work out the page
foreach($users as $k=>$v) {
    if($v->id == $user_id) {
        $page = $k / ROWS_PER_PAGE;
        redirect('users/page/'.$page);
    }
}

Hopefully thats enough to get you in the right direction


Redirecting to the same page after edit - El Forum - 07-28-2009

[eluser]steve_mock[/eluser]
Nice!

insert_id(); is the ticket. That'll come in handy all over the place.

Thank you so much, Dam1an.

-s


Redirecting to the same page after edit - El Forum - 07-29-2009

[eluser]Dam1an[/eluser]
Your welcome Smile
Yeah insert_id is sooo useful