How to display error messages in CodeIgniter

jaypabs picture jaypabs · Aug 24, 2012 · Viewed 31.6k times · Source

In my controller I put this code to check if the shopping cart is empty:

if (!$this->cart->contents()){
    $this->session->set_flashdata('message', 'Your cart is empty!');
}

$this->data['message'] = $this->session->flashdata('message');

$this->load->view('templates/header', $this->data);
$this->load->view('bookings', $this->data);
$this->load->view('templates/footer');

On my "bookings" view I set this code to display the message:

<div id="infoMessage"><?php echo $message;?></div>

But on the first page load the message "Your cart is empty!" is not showing. However, it will display when I press the F5 key or refresh the browser.

I have already read the manual from codeigniter that says "CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared."

However, I don't know where to set this message so it will be available on "next server requrest".

Answer

jleft picture jleft · Aug 24, 2012

There is no need to pass the data to the view and assign it as flashdata.

The reason that it's not working the first time, is that flashdata is only available for the next server request. Loading the views isn't a server request. However, refreshing the page is.

There are a couple of solutions to this:

  • Pass the data directly and load the view. (Not using flash data.)
  • Use flashdata and redirect to the page.

Passing the data directly

You could just pass it directly (personally I'd go with this option - I'm not sure why you'd want to use flashdata in this case, I would've thought that you'd always like to inform the user if their cart is empty...):

Controller:

if (!$this->cart->contents())
{
    $this->data['message'] = 'Your cart is empty!';
}

$this->load->view('templates/header', $this->data);
$this->load->view('bookings', $this->data);
$this->load->view('templates/footer');

View:

<div id="infoMessage"><?php echo $message;?></div>

Using flashdata

Or, if you want to use flashdata, then:

Controller:

if (!$this->cart->contents())
{
    $this->session->set_flashdata('message', 'Your cart is empty!');
}

redirect('/your_page');

View:

 <div id="infoMessage"><?php echo $this->session->flashdata('message');?></div>

If you want to use this method, you can't simply just load the view - due to the server request issue that I explained above - you have to redirect the user, using redirect('/your_page');, and the URL Helper $this->load->helper('url');. You can also send the appropriate HTTP header using the native PHP header function. Loading the view the first time won't work.


You can use this $this->session->keep_flashdata('message'); to preserve the data for an additional request.

I'd also check that your either auto-loading the session library or loading it in the constructor of your controller: $this->load->library('session'); (I'm fairly sure you will be, but it's worth checking anyway.)

Finally, I know that some people who store session data in their database can have issues with flashdata, so that may be worth looking into, if none of the above helps.

On a side note, personally I'd have a check to ensure that a variable is set before echoing it.