CodeIgniter Forums
Shopping Cart - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Shopping Cart (/showthread.php?tid=52316)

Pages: 1 2


Shopping Cart - El Forum - 06-06-2012

[eluser]the_unforgiven[/eluser]
Hi all,

I'm getting the following error:

Code:
A PHP Error was encountered

Severity: Warning
Message:  Invalid argument supplied for foreach()
Filename: inc/header.php
Line Number: 76

On or around the above mentioned line is this:

Code:
<img src="&lt;?php echo base_url(); ?&gt;images/bag.png" width="29" height="29" alt="Cart" />&nbsp;You have: <a href="&lt;?php echo base_url(); ?&gt;">
   &lt;?php $cart = $this->session->userdata('cart');
    if (count($this->session->userdata('cart'))){
    $count = 1;
     foreach ($this->session->userdata('cart') as $PID => $row) {    
        echo $row['count'];
           }  
     }
   ?&gt;
</a> <span>items in your cart</span>

This is the shopping cart for Claudia's Kids but ive changed some of it like instead of having $_SESSION has Ci didn't like it changed to $this->session->...... can someone hlep me please?

Thanks.


Shopping Cart - El Forum - 06-06-2012

[eluser]Tominator[/eluser]
Your problem is, that your $this->session->userdata('cart') is not an array.


Shopping Cart - El Forum - 06-06-2012

[eluser]the_unforgiven[/eluser]
So can you explain how i make it work then please?


Shopping Cart - El Forum - 06-06-2012

[eluser]Tominator[/eluser]
I think your problem is with CI sessions. In fact, I'm not sure if I'm right. But CI is using COOKIEs, and you cannot save array in COOKIEs (I repeat, that I am not sure). It is possible using native SESSIONs.


Shopping Cart - El Forum - 06-06-2012

[eluser]the_unforgiven[/eluser]
NO cookies are stored on this site, all uses sessions via the db


Shopping Cart - El Forum - 06-06-2012

[eluser]Tominator[/eluser]
I cannot help you any more. You have to find why $this->session->userdata(‘cart’) is not an array. Check how are you storing it, check how it's stored and how it's read.


Shopping Cart - El Forum - 06-06-2012

[eluser]the_unforgiven[/eluser]
OK, ill try and figure it myself then Sad


Shopping Cart - El Forum - 06-06-2012

[eluser]beaudierman[/eluser]
This is just a suggestion, but have you tried using CI's built in cart class? It stores all of your cart data in sessions(if enabled, also in your DB) and I found it was a much easier alternative to directly massaging session data.

http://ellislab.com/codeigniter/user-guide/libraries/cart.html


Shopping Cart - El Forum - 06-06-2012

[eluser]the_unforgiven[/eluser]
Hi, yes i'm using the class, that was built for the Claudia's Kids site (a tut someone did a while back)


Shopping Cart - El Forum - 06-06-2012

[eluser]beaudierman[/eluser]
Sorry, maybe I'm not understanding and I'm not familiar with the Claudia's Kids site.

Based on your code it looks like you're just trying to display how many items are in the cart. Which, if using the CI cart class and the example you provided, would be done like this:

Code:
<img src="&lt;?php echo base_url(); ?&gt;images/bag.png" width="29" height="29" alt="Cart" />&nbsp;You have: <a href="&lt;?php echo base_url(); ?&gt;">
&lt;?php $this->cart->total_items(); ?&gt;
</a> <span>items in your cart</span>

Based on the error you got, the session variable "cart" is not an array so it cannot be used in a foreach. Can you show me an example of what that variable contains?