Jump to content

session help


Recommended Posts

ok here is the full code if that helps

 


if(isset($_GET['id'])){
$id = $_GET['id'];

if(isset($_GET['action'])){
$action = $_GET['action'];
}else echo $action="Empty Cart" ;

switch($action){
               case 'add':
		   if(isset($_SESSION['cart'][$id])){
		        $_SESSION['cart'][$id]++;}
		   else $_SESSION['cart'] =1;
		   break;
		   
		   case 'remove':
		   if(isset($_SESSION['cart'][$id])){
		   $_SESSION['cart'][$id]--;
		   if($_SESSION['cart'][$id]==0){
			   unset($_SESSION['cart'][$id]);
			   }
		   }
		   break;
		   
		   case 'delete':
		   unset($_SESSION['cart'][$id]);
		   break;
		   }}

////////////////////////////// Display Shopping Cart /////////////////////////////////
               if(isset($_SESSION['cart'])){
			       $total = 0;
			   foreach($_SESSION['cart'] as $id => $value){
			   }
				   $result = mysql_query("SELECT * FROM products WHERE id=" . $id)or dei(mysql_error());
				   $row = mysql_fetch_array($result);
			       $price = $row['price'];
				   $line_cost = $price * $value;
				   $total = $total+ $line_cost;
				   
			   }print_r($_SESSION['cart']);

Link to comment
Share on other sites

So you understand why your foreach loop was failing then?

 

foreach( 1 as $id => $value )

 

You need to make sure $_SESSION['cart'] is an array before looping through it.

 

 case 'add':
		   if(isset($_SESSION['cart'][$id])){
		        $_SESSION['cart'][$id]++;}
		   else $_SESSION['cart'] =1;
		   break;

is probably the issue.

 

jesi beat me to it.

Link to comment
Share on other sites

$_SESSION['cart'] is still 1. 1 is an integer, and can not be looped through using foreach.

 

We've told you where the issue is, it's up to you to ask the right questions, or fix it.

Link to comment
Share on other sites

Okay so see how cart = 1? That's not an array. An array would be like

[cart] => Array ( [qty] => 1 [id] => 7)

 

We already told you where the problem is, and another person even posted the section of code that is the problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.