• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shopping Cart

#1
[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.

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

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

#4
[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.

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

#6
[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.

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

#8
[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-gui.../cart.html

#9
[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)

#10
[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?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.