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_defn.php');
session_start();
error_reporting(E_ALL);
include('cart_process.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.php 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'] );
}
...
The following example shows how it basically should work:
http://www.jwscripts.com/playground/basket.phps
HTH;
JW
I was wondering if someone could show me the error in my code (see earlier
post) so that I can see what I did wrong.
Thanks
Mark
"Janwillem Borleffs" <j...@jwscripts.com> wrote in message
news:40fa969d$0$146$1b2c...@news.wanadoo.nl...
In this code, you show an attempt to add an element to $order. But where did
$order come from? Where did you initialize it by pulling it back out of the
stored SESSION data?
> Here is some code for cart_process.php 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?
> }
Perhaps the problem is right here. You don't pull the existing cart out of
the session to work with it and I don't see any other references to the cart
in the SESSION array.
> if ( isset( $_POST['addtobasket'] ) ) { // if form submitted
> $_SESSION['basket']->add_item( $_POST['item'], $_POST['qty'] );
> }
Was this supposed to be referencing 'cart' instead of 'basket'?
- Virgil
The difference between your code and mine, is that the latter verifies
whether a product has been ordered before it is added to the array.
When already defined, the quantity is increased. Otherwise, the item is
added.
BTW, the real problem with your code is the following line:
$order[] = $orderline;
In this context, $order is limited to the function's namespace. To use
it class wide, you should declare the $order variable outside the
function and access it as follows:
$this->order[] = $orderline;
But, parsing will be more straightforward when using the approach from
my example.
JW
Mark
"Janwillem Borleffs" <j...@jwscripts.com> wrote in message
news:40FBCD90...@jwscripts.com...
Mark
"Virgil Green" <v...@DESPAMobsydian.com> wrote in message
news:pRPKc.16190$HG4....@newssvr24.news.prodigy.com...
YES!
> - Virgil
>
>