Welcome Guest, Not a member yet? Register   Sign In
Problem with flashdata

I've got a php5/apache2.0/mysql set up on my local box and I'm trying to follow some code elsewhere but I've got a problem with flashdata. Below is a model designed to update a shopping cart and, two line from the end, set flashdata to say cart has been updated ... I added the echo afterwards just to make sure the code was being executed - it is! ...
function updateCart($productid, $fullproduct)
        $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();    //pull in existing cart first - copy Session cart to working var $cart!
        $totalprice = 0;
        if (count($fullproduct))
                if (isset($cart[$productid]))    //check to see if product already exists in cart, if it does, add 1 to count
                        $prevct = $cart[$productid]['count'];
                        $prevname = $cart[$productid]['name'];
                        $prevprice = $cart[$productid]['price'];
                        $cart[$productid] = array(
                            'name' =>$prevname,
                            'price' =>$prevprice,
                            'count' =>$prevct + 1
                        $cart[$productid] = array(
                            'name' =>$fullproduct['name'],
                            'price' =>$fullproduct['price'],
                            'count' =>1
                // Loop through cart adding up total Cost
                foreach ($cart as $id =>$product)
                        $totalprice += $product['price'] * $product['count'];
                $_SESSION['totalprice'] = $totalprice;
                $_SESSION['cart'] = $cart;    //Set Session cart to the working var $cart
                $this->session->set_flashdata('conf_msg', "We've added this product to your    cart.");
                echo "inside flash: ". $this->session->flashdata('conf_msg');                

Here's the relevant part of the controller:
function cart($productid=0)
        if ($productid > 0)
                $fullproduct = $this->MProducts->getProduct($productid);
                redirect('welcome/product/'.$productid, 'refresh');

Here's the relevant part of the view:
if ($this->session->flashdata('conf_msg'))
            echo "<div class='message'>message";
            echo $this->session->flashdata('conf_msg');
            echo "</div >";
Anyone know what I'm doing wrong?


Since you haven't said what's going wrong, and what you expect to happen, is it safe to assume that in your view your conf_msg is empty?

Er, yeah ... the flashdata isn't displaying in the view ...

Sorry for the dopiness!

Flashdata has some problems when it comes to displaying messages. An alternative is to do something as follows:

// in controller
$response['message'] = 'Item successfully added';
$this->load->view('myview', $response);

// in view
if(isset($message)) { echo $message; }

Are you doing any redirects - or any thing that would cause an extra page load in there somewhere?

That's been the only 'problem' I've had with flashdata, where I've then just had to make use of $this->session->keep_flashdata('thingy');

Thanks guys,

I'm very new to CI so any help appreciated ... in answer to the question,
Yes, I am redirecting after the cart update ...

I redirecto to a 'product' controller - that is the controller loads which (re)loads the product view after the product is added to the cart and it should redisplay the product and the message 'cart updated'

so, should I be passing a message back from the updatecart controller to the product controller? if so, how do I do that?

or, if I need to use the flash-keepdata method, in which controller would I put it?

thanks in advance.

Well, the CI User Guide (in the [url="http://localhost/devel/ci_user_guide/libraries/sessions.html"]Session Library[/url] page makes it quite clear:

Quote:CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared

So, you need to keep the data using the keep_flashdata() function, and the best place to do that would be immediately before the redirect. You could check if it exists already, but I think a keep_flashdata against non-existent data works just as well (ie. doesn't report any errors) - so it might be faster to just do the keep all the time. Your call on performance/elegance there.

EDIT: alternatively, use real session data rather than flashdata - you'd have to manage your session data sensibly there, culling it when done, checking age and validity before usage, etc - but it might turn out to be a neater approach.

Mmmm, still no joy.

I've added this code immediately before the redirect:


but it still doesn't show up in the view although it gets set in the model.

Any other ideas?


Quote:Any other ideas?

Nah .. I only ever have the one idea at a time.

I suspect this is the problem, though, and you're missing one of the redirects somewhere. Perhaps you could put a log feature in your constructor to dump out a date stamp and the contents of that variable to a file somewhere ... just to satisfy yourself that you don't have an additional redirect or remap etc.

Did you try putting the data into real session, rather than flashdata, and confirm you can see it later? It shouldn't be a session related issue, but just in case - and that would prove out that your loading and usage of the session library was tickety-boo.

Theme © iAndrew 2016 - Forum software by © MyBB