Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:31:16+00:00 2026-06-10T05:31:16+00:00

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

  • 0

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”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:31:18+00:00Added an answer on June 10, 2026 at 5:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Where i should put this code in my app to reset tab bar controller
I searched on Google, and I got this code to check if a controller
in this tutorial of Yii framework http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action i want to put my Actions from
Where do you put non-controller, non-model code, like util classes, extension methods and so
I put a TCP server and client code in PIC controller. Now just wanted
Zend recommends to put controller action helpers in the mvc into /application/controllers/helpers . Is
I put a breakpoint on the controller action but it never fires. Is there
If i put an object named foo into model in Spring controller, and want
I have view controller, into the view i have put a table view, and
Is anyone used put, get, post, delete annotations(https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Controller/Annotations/) in controller. I'm trying to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.