PHP cart with cookies only

Associate
Joined
19 Jul 2006
Posts
1,847
Bit stumped with this, I have to make a shopping cart that uses cookies not sessions. Ever tutorial I have found uses sessions for this. Does anyone know of a tutorial that just uses cookies.
Or is it possible to more or less just swap the session for cookie?
 
yeah its for a course im doing. otherwise id have done it as a session. But were not allowed to do sessions for this assignment
 
sorry but i cant get that to work
PHP:
<?php 
$fakeSession = array();
$fakeSession['foo'] = 50;
$fakeSession['bar'] = 'Something';
$data = serialize($fakeSession);
setcookie("item",$data,time()+3600);?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	

</head>
<body>

	<?php $fakeSession = unserialize($_COOKIE['item']);
print $fakeSession['foo'];?>
	<p>No cart available.</p>
	

</body>
</html>

I run this page and then reload it just to make sure that the cookies are set but nothing, all i get is "no cart available"
 
Last edited:
Im getting there but now i have a problem with associated arrays.
PHP:
<?php
@$new=$_POST['item'];
if($new) {
if(!isset($_COOKIE["cart"])){
$Scart = array();
$Scart[$new] = "1";
$serial = serialize($Scart);
setcookie("cart","$serial",time()+100000);
}
if(isset($_COOKIE["cart"])){
$Scart = array();
$Scart = unserialize(stripslashes($_COOKIE["cart"]));
$Scart[$new] = "1";
$serial = serialize($Scart);
setcookie("cart","$serial",time()+100000);
}
}
?>

to get output im using
PHP:
print_r (unserialize(stripslashes($_COOKIE["cart"])));
Which gives Array ( [tshirt] => 1 [jumper] => 1 )

So I create an array with the Scart with a entry "item" which is the name of the product been sent from the form on previous page and set it to 1 (quantity)

But now i need to put this output into a table with |product|Quantity(that can be changed| and update.

so how do i get each product in its own row and how do i search through the variable to be able to update the quantity later?

TIA
 
Not allowed to make any more tables in the database to store the cart data.
I can do the update now but still having trouble with the associated arrays
 
Back
Top Bottom