Codeigniter - SPLessons

CodeIgniter Flashdata

Home > Lesson > Chapter 27
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Flashdata

Flashdata in CodeIgniter

shape Description

Flashdata in CodeIgniter prepares the necessary data for the next coming request from server. Flashdata takes the help of Session. But, in this session, the passed data can be stored only once till the next server request is given response and then cleared automatically. There are two functions available to add Flashdata in CodeIgniter. Hence, Flashdata is mostly used to display messages and notifications on the next page – like errors, success/warning messages and status messages. Prerequisite The session library should already be loaded via config/autoload.php or manually in the controller.

Add Flashdata

shape Description

mark_as_flash()

The function mark_as_flash() is used to add flashdata in CodeIgniter which has only single argument item value. Multiple values can also be added as an array. [php]$this->session->mark_as_flash('item');[/php]

set_flashdata()

set_flashdata() has two arguments. One is name of item name and other is the value. [php] $this->session->set_flashdata('item','value'); [/php]

Retrieve Flashdata

The function flashdata() has only single parameter which is given to item that need to be fetched. [php]$this->session->flashdata('item');[/php] If no parameter is passed, then an array with same function can be obtained.

shape Example

Create the controller Flashcontroller.php in the file path application/controllers and enter the below code. [php] <?php class Flashcontroller extends CI_Controller { public function index() { //Load session library $this->load->library('session'); //redirect to home page $this->load->view('flash_message'); } public function add() { //Load session library $this->load->library('session'); $this->load->helper('url'); //add flash data $this->session->set_flashdata('item','splessons'); //redirect to home page redirect('flashdata'); } } ?> [/php] Create a view flash_message.php in the file path application/views and enter the below code. [php] <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>CodeIgniter Flashdata Example</title> </head> <body> Flash Data Example <h2><?php echo $this->session->flashdata('item'); ?></h2> <a href = 'flashdata/add'>Click Here</a> to add flash data. </body> </html> [/php] Add the route to the above created controller in routes.php file present in application/config and enter the below code at the end of the file. [php] $route['flashdata'] = 'Flashcontroller'; $route['flashdata/add'] = 'Flashcontroller/add'; [/php] Now check the output by entering the URL, [php] http://localhost/Codeigniter/index.php/flashdata [/php] Output: When clicked on Click Here button, the output with the flashdata appears. As the flashdata variable is removed automatically, if refreshed the page, the first output is only seen instead of second output.

Summary

shape Key Points

  • Flashdata in CodeIgniter prepares the necessary data for the next coming request from server.
  • The function mark_as_flash() is used to add flashdata in CodeIgniter.