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
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.