Seeking intermediate session tutorial - Cart related

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
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

Seeking intermediate session tutorial - Cart related

Post by rxsid »

Hi all,

I'm strugling a bit here to get this sessions based shopping cart code to work. I have looked at and installed zencart as an option, but it was way overkill (IMHO) for what I need. Basically, I'm looking to have items added to a user's session and keep track of the info in a db as well (the db part isn't an issue for me).

Here's what I'm looking to track and be able to display in the user's 'cart' page:

Code: Select all

$_SESSION['cart']['itemNumber'] = $itemNumber;
$_SESSION['cart']['itemQty'] = $itemQty;
$_SESSION['cart']['itemDesc'] = $itemDesc;
$_SESSION['cart']['itemPrice'] = $itemPrice;
What i'm having an issue with is when more than one item is added, or existing item(s) updated. I can only get 1 item and it's info there to be displayed.

the code snippet below is total junk, and has been hacked at for some time now...but this is the sort of thing I've been trying:

Code: Select all

session_start();


if ($cartAction == "add" && ($_SESSION['cart']['c_itemNumber'])) {
	$tempCartItemNumber = "";
	foreach ($_SESSION['cart']['c_itemNumber'] as $cItNu) {
		if ($cItNu != $itemNumber) {
			$tempCartItemNumber = ",".$itemNumber;
		} else {
			$tempCartItemNumber .= $cItNu;
		}
	}
} else {
	$_SESSION['cart']['c_itemNumber'] = $itemNumber;
}

echo $_SESSION['cart']['c_itemNumber'];
So, at this point, what I would like to do is find a straight forward tutorial on how to build a 'basic' shopping cart that would mimic (if possible) the $_SESSION['cart']['items'] method. Once I can get the add,update,delete session stuff to work....I'll be able to integrate the db part into it. Googling this sort of thing returns thousands of results that i've tried to check out, but haven't found anything besides the complex session cart stuff (like zencart) or the super basic stuff like how to start and destroy a session. Would anyone be able to provide a link to such a tutorial for like what I'm trying to do? It would be greatly appreciated.

Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It might simplify your code of you deal with data structures directly and then get/set them in the session before/after like:

Code: Select all

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();

// my code here

$_SESSION['cart'] = $cart;
Or:

Code: Select all

process_cart($_SESSION['cart']);
(#10850)
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

Post by rxsid »

thanks for the reply and the info arborint. I do appreciate it! :D

i wound up discovering this Building A Persistent Shopping Cart With PHP and MySQL article by accident (searching for another topic), that I've modeled my situation after. I dumped the idea of storing anything but the item identifying (itemNumer) and the actual built in session ID within the session. all other things I wanted to track I'm just using a db.
Post Reply