⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

[WORKAROUND] Redirect to the list after insert/update without changing the core functionality of GC



Neeraj Singh
  • profile picture
  • Member

Posted 20 June 2016 - 12:41 PM

Well here is simple example to demonstrate, How can we redirect user by using $crud->callback_after_insert() with last insert id or primary key.


<?php (defined('BASEPATH') or exit('No direct script access allowed'));

/**
 * Test Class to show grocery CRUD redirection
 * after insert records with last insert id
 *
 */
class GC_Redirection extends CI_Controller
{
    /**
     * Just to store CRUD instance
     * @var null
     */
    private $crud = null;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Auto call method
     * @return [type] [description]
     */
    public function employees_management()
    {
        $crud = new grocery_CRUD();

        // store crud instance in class property, coz you cant all $crud->set_lang_string inside callback function
        $this->crud = $crud;

        $crud->set_theme('datatables');
        $crud->set_table('employees');
        $crud->set_relation('officeCode', 'offices', 'city');
        $crud->display_as('officeCode', 'Office City');
        $crud->set_subject('Employee');
        $crud->required_fields('lastName');
        $crud->set_field_upload('file_url', 'assets/uploads/files');

        // callback function to open/redirect add question page
        $crud->callback_after_insert(array($this, '_open_your_redirection_page'));

        $output = $crud->render();
        $this->_example_output($output);
    }

    /**
     * Show add Question form after Create QSets
     * and pass qna_set ID in URL
     * @param [type] $post_array [description]
     * @param [type] $primary_key [description]
     * @return [type] [description]
     */
    public function _open_your_redirection_page($post_array, $primary_key)
    {
        // create a full redirection link with primary key (last insert id)
        $link = site_url(strtolower(__CLASS__) . "your_method/" . $primary_key);
        // html and javascript code
        $data = "Your data has been successfully stored into the database.<br/>Please wait while you are redirecting to the another page.";
        $data .= "<script type='text/javascript'> window.location = '" . $link . "';</script><div style='display:none'></div>";
        
        // set it in gc crud
        $this->crud->set_lang_string('insert_success_message', $data);

        // return array to complete action
        return $post_array;
    }
}


sfthurber
  • profile picture
  • Member

Posted 04 October 2016 - 15:28 PM

why not just do this:
->set_lang_string(  'insert_success_message','Step 1 has completed; wait a moment please for the next step to display.
<script type="text/javascript">
$("#this_page_body").fadeOut(300);
window.location = "'.site_url(strtolower(__CLASS__).'/'.strtolower(__FUNCTION__)).'-rd'.'";
</script>' 
->callback_after_insert(array($this, 'callback_after_insert_rd')

 

along with this:

function callback_after_insert_rd($post_array, $primary_key) { 
$this->session->set_userdata('added-id', $primary_key); 

}

 

so if the crud is controller: foo function: bar

then redirect will be to controller: foo function:bar-rd

and the bar-rd function can get the id of the added record using the session class and then do whatever you want 


Despi81
  • profile picture
  • Member

Posted 08 May 2017 - 20:35 PM

 

along with this:

function callback_after_insert_rd($post_array, $primary_key) { 
$this->session->set_userdata('added-id', $primary_key); 

}

 

so if the crud is controller: foo function: bar

then redirect will be to controller: foo function:bar-rd

and the bar-rd function can get the id of the added record using the session class and then do whatever you want 

 

 

Hi,

 

sorry, but how do I access the session info then?

If I use e.g.

var_dump($this->session->get_userdata('added-id'));

 

I get the error:

Severity: Notice

Message: Undefined property: Main::$session

 

Thank you!


riham13
  • profile picture
  • Member

Posted 13 December 2017 - 05:15 AM

you have to use "set_lang_string" into your controller not in the callback.


luismiguells
  • profile picture
  • Member

Posted 06 September 2018 - 14:24 PM

I know this is not a proper way to do it but if you want to have a redirection add insert and/or update operation without changing the functionality of grocery CRUD you can simply do:

for insert:

   $crud->set_lang_string('insert_success_message',
		 'Your data has been successfully stored into the database.<br/>Please wait while you are redirecting to the list page.
		 <script type="text/javascript">
		  window.location = "'.site_url(strtolower(__CLASS__).'/'.strtolower(__FUNCTION__)).'";
		 </script>
		 <div style="display:none">
		 '
   );
for update:
   $crud->set_lang_string('update_success_message',
		 'Your data has been successfully stored into the database.<br/>Please wait while you are redirecting to the list page.
		 <script type="text/javascript">
		  window.location = "'.site_url(strtolower(__CLASS__).'/'.strtolower(__FUNCTION__)).'";
		 </script>
		 <div style="display:none">
		 '
   );

It works great but if you edit an entry it send you to the redirect page. How can I solve this problem? I want only redirect when I insert a new entry I alredy try with "callback_after_insert" but didn't work.