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