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'] );
}
.... 6 2174
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
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
"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
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex Hopson |
last post by:
Hi,
I'm trying to modify a shopping cart script from Mastering PHP/MySQL and am
having trouble setting up some arrays for it. The original code, below,
stores the cart items in a session...
|
by: Paul Bruneau |
last post by:
Hi,
I hope someone can help me make a working shopping cart, as a learning tool.
If I have a "Product Demo" html page with a "Buy Me" button, there must be
a simple javascript method of...
|
by: madison |
last post by:
Hi,
I am trying to start a website using paypals shopping cart function.
If i have 10 items and they sell out, how do I make it so the item is
then listed as sold out. The next person would not...
|
by: Tulasi |
last post by:
Hello, any one help me the problem due to Shopping cart.
I am developping a project in that project,I want to connect shopping Carts
in Vb.net.The shopping carts...
|
by: G.E.M.P |
last post by:
High Level Session Handling Design for a Shopping cart
0) What am I missing?
1) How does OSCommerce do it?
I'm thinking about building a shopping cart from scratch,
using a library of dynamic...
|
by: MrL8Knight |
last post by:
Hello,
I am trying to build a simple php form based shopping cart using a cookie with arrays. I need to use 1 cookie because each order will have over 20 items. With that said, I realize I need to...
|
by: jecha |
last post by:
I'm implementing a shopping cart but am having a problem in checking out a person who has added item in his/her shopping busket.The code for the checkout.php script is given below
<?...
|
by: judge82 |
last post by:
Please I need help with this so bad. I have been struggling with it for 2weeks now.
on line 206, I what to create a link that will direct you to the detail of the chosen items, like in the...
|
by: Paulo |
last post by:
Hi, beginner on asp.net 2.0 C# VS 2005, how can I use the shopping cart
concept on my application? When the user clicks add item, it will be stored
on some storage format, I dont know what is the...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |