Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem appending arrays in shopping cart

0 views
Skip to first unread message

Fnark!

unread,
Jul 18, 2004, 10:15:15 AM7/18/04
to
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_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'] );
}
...


Janwillem Borleffs

unread,
Jul 18, 2004, 11:26:19 AM7/18/04
to
Fnark! wrote:
> 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.
>

The following example shows how it basically should work:

http://www.jwscripts.com/playground/basket.phps


HTH;
JW

Mark

unread,
Jul 19, 2004, 8:41:21 AM7/19/04
to
Thanks for that. I will look over the code. That code looks quite similar to
mine in some ways - at first glance I can't see what I have done wrong in my
code that it should not work whereas yours does.

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...

Virgil Green

unread,
Jul 19, 2004, 9:17:09 AM7/19/04
to
"Fnark!" <noon...@fakoaddresso.com> wrote in message
news:TBvKc.63370$q8.1...@fe1.news.blueyonder.co.uk...

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


Janwillem Borleffs

unread,
Jul 19, 2004, 9:33:04 AM7/19/04
to Mark
Mark wrote:
> Thanks for that. I will look over the code. That code looks quite similar to
> mine in some ways - at first glance I can't see what I have done wrong in my
> code that it should not work whereas yours does.
>
> 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.
>

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

unread,
Jul 20, 2004, 5:56:51 AM7/20/04
to
Thanks. I took on board your advice and now the sessions are working
correctly. Thank you very much for your helpful suggestions!

Mark

"Janwillem Borleffs" <j...@jwscripts.com> wrote in message

news:40FBCD90...@jwscripts.com...

Mark

unread,
Jul 20, 2004, 5:56:52 AM7/20/04
to
Thanks for your help Virgil. The sessions are working correctly now.

Mark

"Virgil Green" <v...@DESPAMobsydian.com> wrote in message
news:pRPKc.16190$HG4....@newssvr24.news.prodigy.com...

YES!

> - Virgil
>
>


0 new messages