Hi,
I'm trying to write a little shopping cart for a client, and I've decided to use sessions to store the item data. I've created an simple objects class with a constructor and a few accessors, just to hold the data in.
I wrote a script to test the sessions, but it doesn't seem to be working right. Everytime I test the session data (I just fill a dummy Item object), it just seems to overwrite itself. I've got --enable-track-vars on, so I dont think that's what's causing the problem.
Anyone have any idea what I'm doing wrong? Here's some of my code -
<?php
// Cart Checkout, View, Modify and Review Script
// Written by Alex Augot - alexaugot@future-wave.com
// -------------------------------------------------
// Ok, so this is the big chunk of code. It allows
// the user to view and modify their shopping cart
require("cart_class_include.php");
require("cart_session_include.php");
switch($action){
case "add_to_cart":
add_to_cart($prev_page, $cust_id, $item_name, $item_description, $item_price, $item_quanity);
break;
default:
break;
}
function add_to_cart($prev_page, $cust_id, $item_name, $item_description, $item_price, $item_quanity){
if($s_cust_id != $cust_id){
print("1 $s_cust_id<br>");
print("2 $cust_id<br><br>");
print("There has been an error in the shopping cart system. Please go <a href=\"$prev_page\">back
and try again");
exit();
}
if($HTTP_SESSION_VARS[count($HTTP_SESSION_VARS)] = new Item($item_name, $item_description, $item_price, $item_quanity, $prev_page)){
print("Item added correctly<br><br>");
}
for($temp1=0; $temp1 < count($HTTP_SESSION_VARS); $temp1++){
$temp_obj = $HTTP_SESSION_VARS[$temp1];
if(is_object($temp_obj)){
$temp_obj->printReport();
}else{print("$temp-obj");}
}
}
?>
<?php include("cart_add_include.php"); ?>
<?php
session_start();
/*
// Cart Session Include Script. Must be included on EVERY page
// Written by Alex Augot - alexaugot@future-wave.com
// Variable Declerations
// s_cust_id - The unique ID of the customer
*/
if(!(session_is_registered('s_cust_id'))){
$s_cust_id = session_id();
$s_remote_ip = $REMOTE_ADDR;
session_register('s_cust_id');
session_register('s_remote_ip');
}else{
print("Printing session id - $s_cust_id<br><br>");
}
print("<!-- BEGINNING CART CODE -->");
print("<!-- ENDING CART CODE -->");
?>
<?php
// Cart Add Script.
// Written by Alex Augot - alexaugot@future-wave.com
$item_name = "Testing";
$item_description = "Jack White";
$item_price = "3.23";
?>
<form method="post" action="http://www.eyedealpostcards.com/shoppingCart/cart.php">
<input type="hidden" name="action" value="add_to_cart">
<input type="hidden" name="prev_page" value="<?php print("http://" . $HTTP_HOST . $SCRIPT_NAME); ?>">
<input type="hidden" name="s_cust_id" value="<?php echo $s_cust_id; ?>">
<input type="hidden" name="item_name" value="<?php echo $item_name; ?>">
<input type="hidden" name="item_description" value="<?php echo $item_description; ?>">
<input type="hidden" name="item_price" value="<?php echo $item_price; ?>">
Quanity - <input type="text" name="item_quanity" size="5" maxlength="7">
<input type="submit" name="submit_me" value="Add To Cart">
</form>