469,315 Members | 1,619 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

Problem appending arrays in shopping cart

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'] );
}
....
Jul 17 '05 #1
6 2073
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

Jul 17 '05 #2
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" <jw@jwscripts.com> wrote in message
news:40*********************@news.wanadoo.nl...
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

Jul 17 '05 #3
"Fnark!" <no*******@fakoaddresso.com> wrote in message
news:TB******************@fe1.news.blueyonder.co.u k...
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
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
Jul 17 '05 #4
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

Jul 17 '05 #5
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" <jw@jwscripts.com> wrote in message
news:40**************@jwscripts.com...
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

Jul 17 '05 #6
Thanks for your help Virgil. The sessions are working correctly now.

Mark

"Virgil Green" <vj*@DESPAMobsydian.com> wrote in message
news:pR******************@newssvr24.news.prodigy.c om...
"Fnark!" <no*******@fakoaddresso.com> wrote in message
news:TB******************@fe1.news.blueyonder.co.u k...
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


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'?


YES!
- Virgil

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Alex Hopson | last post: by
2 posts views Thread by Paul Bruneau | last post: by
1 post views Thread by madison | last post: by
2 posts views Thread by G.E.M.P | last post: by
1 post views Thread by jecha | last post: by
3 posts views Thread by judge82 | last post: by
3 posts views Thread by Paulo | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.