Help on Simple Shopping Cart..[SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Help on Simple Shopping Cart..[SOLVED]

Post by Zoxive »

Well im working on the very beginning of my shopping cart, for a website im making for a compitition for school. And i just started making the shopping cart, its and the very early stages, and right now im making it so it saves everything in your cookies. In a line, and using regex to split them.. well here just check out my code so far..

Code: Select all

<?php
//include('../includes/data.php');

if(isset($_COOKIE['cart']) & $_COOKIE['cart']!='' & $_GET['add']==''){
	prin();
}

if(isset($_GET['add'])){
	$add=$_GET['add'];
	encode($add);
	prin();
}

function prin(){
	$cart = preg_split('/&/', $_COOKIE['cart']);
	print '<pre>';
	print_r ($cart);
	print '</pre>';
}

function encode($new){
	if(isset($_COOKIE['cart']) & $_COOKIE['cart']!=''){
		$cart=$_COOKIE['cart'];
		$cart=$cart . '&' . $new;
		setcookie("cart", $cart);
	}else{
		setcookie("cart", $new);
	}
	
}

print '<br><br><br><a href="remove.php">Remove cookie</a>';

?>

The problem im having is, when i do index.php?add=1 , it will show

Code: Select all

Array
(
    [0] => 
)
Untill i do it again, then it will show the 1 at the end, like it should, but if i do that, and close the page, open it again, it will show the 1, or if there are 3 items, and i close it and reload it again it will show 4, try it your self, you will see what i mean.

I just want it to show up the first time.. : p

Thanks..

-NSF
Last edited by Zoxive on Wed Nov 02, 2005 8:01 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

A cookie requires a refresh before you may access it.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Jcart wrote:A cookie requires a refresh before you may access it.
So what would be an easyer way to do it?
Store it in session? temporarly?
then also call for setting it at a cookie? incase they close the page?

Edit:
Yup, just added in sessions and it works perfectly : p

Code: Select all

<?php
//include('../includes/data.php');

if(isset($_COOKIE['cart']) & $_COOKIE['cart']!='' & $_GET['add']==''){
	$_SESSION['cart']=$_COOKIE['cart'];
	printer();
}

if(isset($_GET['add'])){
	$add=$_GET['add'];
	encode($add);
	printer();
}

function printer(){
	$cart = preg_split('/&/', $_SESSION['cart']);
	print '<pre>';
	print_r ($cart);
	print '</pre>';
}

function encode($new){
	if(isset($_COOKIE['cart']) & $_COOKIE['cart']!=''){
		$cart=$_COOKIE['cart'];
		$cart=$cart . '&' . $new;
		$_SESSION['cart']=$cart;
		setcookie("cart", $cart);
	}else{
		$_SESSION['cart']=$new;
		setcookie("cart", $new);
	}
	
}

print '<br><br><br><a href="remove.php">Remove cookie</a>';

?>
-NSF
Post Reply