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'] );
}
.... 6 2265
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.c om> wrote in message
news:40******** *************@n ews.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*******@fako addresso.com> wrote in message
news:TB******** **********@fe1. news.blueyonder .co.uk... 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
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.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? }
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.c om> wrote in message
news:40******** ******@jwscript s.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*@DESPAMobsy dian.com> wrote in message
news:pR******** **********@news svr24.news.prod igy.com... "Fnark!" <no*******@fako addresso.com> wrote in message news:TB******** **********@fe1. news.blueyonder .co.uk... 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
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.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? }
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 variable array
($HTTP_SESSION_VARS = array();) basically this stores an associative
array with the id number of the product and the qty (ie cart =
qty of that product).
This works fine, but I need to be able to have sub options AND colours for
|
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 storing the necessary product information. There
could be several fields involved...
Then there ought to be another javascript method that can create a new
document based on the list a user has built selecting product from several
|
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 be able to come
along and add it to their shopping cart.
thanks
joy
|
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
like(www.oscommerce.com),(www.miva.com) and Yahoo! Stores.And Retrievs the
Shopping cart Coustmer information and Product information Etc. how it is
connected?How the information is retrived those Shopping cart?
What is the procedure to connecect those Shopping Carts?Any one to...
|
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 screen generation routines
(already written) that take an XML stream as input from
various "search for products" forms. That way I can
run queries in one window and display the dynamic
results in another. The searching functions will probably
| |
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 serialize the data to put the array into the cookie. That part of my code is working just fine and displaying fine. The problem I’m having is when I try to unserialize and display; the data does not appear. If I remove my unserialize command line (see...
|
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
<?
require_once('functions.inc.php');
session_start();
do_html_header("Checkout");
$cart = $_SESSION;
if($cart&&array_count_values($cart))
{
display_cart($cart,false,0);
display_checkout_form($HTTP_POST_VARS);
|
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 screenshot
http://img266.imageshack.us/img266/8673/clipimage002oz4.jpg
Yes, I know the rules. This is a home and the class is for graduate student and I am an undergraduate. the teacher doesn't render any help whatsoever. check the comment on her...
|
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 term: temp/global DataSet,
etc... and it should be read on other web-form to checkout the items...
Can you point me to the right direction?
Thank you very much!
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |