Jump to content


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

list redirect insert redirect update redirect

  • Please log in to reply
23 replies to this topic

#21 Neeraj Singh

Neeraj Singh

    Newbie

  • Members
  • Pip
  • 3 posts
  • LocationMumbai

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;
    }
}


Edited by Neeraj Singh, 20 June 2016 - 12:55 PM.


#22 sfthurber

sfthurber

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 04 October 2016 - 03: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 



#23 Despi81

Despi81

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 08 May 2017 - 08: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!



#24 riham13

riham13

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 13 December 2017 - 05:15 AM

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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users