Jump to content

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
https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342003
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
https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342057
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.