I am creating a shopping cart using PHP Version 4.1.2. I am creating and
registering a cart object in a session. The cart object contains an array of
arrays called $order whose elements are a collection of $orderline
associative arrays which, in turn, hold the global POST values key
'order_code' and value 'qty' as passed in from another page.
My problem is (shown by using print_r to print out the contents of the
arrays) each time I submit new $_POST['order_code'] and $_POST['qty']
values to the cart.php page, the $order array is always overwritten with the
new values, not appended so there is always only one $orderline element
instead of many.
My understanding from the php.net manual is that using this notation,
$order[] = $orderline; (see below and ) should increase the index by one and
add the new element to the array.
Can someone tell me why I am not experiencing this effect? Thanks.
On the cart.php I have:
<php
include('cart_d efn.php');
session_start() ;
error_reporting (E_ALL);
include('cart_p rocess.php');
...
cart_defn.php defines class Cart which includes a function to add items:
class Cart {
function add_item( $order_code, $qty ) {
// create a new orderline
$orderline = array( $order_code => $qty );
// add orderline to order
$order[] = $orderline; // should increment max array index by 1 and
add element to array?
}
...
} // end of class Cart
Here is some code for cart_process.ph p which registers the Cart object in
the session and calls the add_item() function.
....
if ( !isset( $_SESSION['cart] ) ) {
$cart = new Cart;
// register cart in session
$_SESSION['cart'] = $cart; // correct notation?
}
if ( isset( $_POST['addtobasket'] ) ) { // if form submitted
$_SESSION['basket']->add_item( $_POST['item'], $_POST['qty'] );
}
....
registering a cart object in a session. The cart object contains an array of
arrays called $order whose elements are a collection of $orderline
associative arrays which, in turn, hold the global POST values key
'order_code' and value 'qty' as passed in from another page.
My problem is (shown by using print_r to print out the contents of the
arrays) each time I submit new $_POST['order_code'] and $_POST['qty']
values to the cart.php page, the $order array is always overwritten with the
new values, not appended so there is always only one $orderline element
instead of many.
My understanding from the php.net manual is that using this notation,
$order[] = $orderline; (see below and ) should increase the index by one and
add the new element to the array.
Can someone tell me why I am not experiencing this effect? Thanks.
On the cart.php I have:
<php
include('cart_d efn.php');
session_start() ;
error_reporting (E_ALL);
include('cart_p rocess.php');
...
cart_defn.php defines class Cart which includes a function to add items:
class Cart {
function add_item( $order_code, $qty ) {
// create a new orderline
$orderline = array( $order_code => $qty );
// add orderline to order
$order[] = $orderline; // should increment max array index by 1 and
add element to array?
}
...
} // end of class Cart
Here is some code for cart_process.ph p which registers the Cart object in
the session and calls the add_item() function.
....
if ( !isset( $_SESSION['cart] ) ) {
$cart = new Cart;
// register cart in session
$_SESSION['cart'] = $cart; // correct notation?
}
if ( isset( $_POST['addtobasket'] ) ) { // if form submitted
$_SESSION['basket']->add_item( $_POST['item'], $_POST['qty'] );
}
....
Comment