Problem appending arrays in shopping cart

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fnark!

    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_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'] );
    }
    ....


  • Janwillem Borleffs

    #2
    Re: Problem appending arrays in shopping cart

    Fnark! wrote:[color=blue]
    > 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.
    >[/color]

    The following example shows how it basically should work:




    HTH;
    JW



    Comment

    • Mark

      #3
      Re: Problem appending arrays in shopping cart

      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:40fa969d$0 $146$1b2cd167@n ews.wanadoo.nl. ..[color=blue]
      > Fnark! wrote:[color=green]
      > > 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.
      > >[/color]
      >
      > The following example shows how it basically should work:
      >
      > http://www.jwscripts.com/playground/basket.phps
      >
      >
      > HTH;
      > JW
      >
      >
      >[/color]


      Comment

      • Virgil Green

        #4
        Re: Problem appending arrays in shopping cart

        "Fnark!" <noonehere@fako addresso.com> wrote in message
        news:TBvKc.6337 0$q8.15474@fe1. news.blueyonder .co.uk...[color=blue]
        > 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[/color]
        of[color=blue]
        > 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[/color]
        the[color=blue]
        > 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[/color]
        and[color=blue]
        > 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[/color]
        and[color=blue]
        > add element to array?
        > }
        > ...
        > } // end of class Cart[/color]

        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?

        [color=blue]
        > 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?
        > }[/color]

        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.
        [color=blue]
        > if ( isset( $_POST['addtobasket'] ) ) { // if form submitted
        > $_SESSION['basket']->add_item( $_POST['item'], $_POST['qty'] );
        > }[/color]

        Was this supposed to be referencing 'cart' instead of 'basket'?

        - Virgil


        Comment

        • Janwillem Borleffs

          #5
          Re: Problem appending arrays in shopping cart

          Mark wrote:[color=blue]
          > 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.
          >[/color]

          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

          Comment

          • Mark

            #6
            Re: Problem appending arrays in shopping cart

            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:40FBCD90.5 050506@jwscript s.com...[color=blue]
            > Mark wrote:[color=green]
            > > Thanks for that. I will look over the code. That code looks quite[/color][/color]
            similar to[color=blue][color=green]
            > > mine in some ways - at first glance I can't see what I have done wrong[/color][/color]
            in my[color=blue][color=green]
            > > code that it should not work whereas yours does.
            > >
            > > I was wondering if someone could show me the error in my code (see[/color][/color]
            earlier[color=blue][color=green]
            > > post) so that I can see what I did wrong.
            > >[/color]
            >
            > 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
            >[/color]


            Comment

            • Mark

              #7
              Re: Problem appending arrays in shopping cart

              Thanks for your help Virgil. The sessions are working correctly now.

              Mark

              "Virgil Green" <vjg@DESPAMobsy dian.com> wrote in message
              news:pRPKc.1619 0$HG4.4916@news svr24.news.prod igy.com...[color=blue]
              > "Fnark!" <noonehere@fako addresso.com> wrote in message
              > news:TBvKc.6337 0$q8.15474@fe1. news.blueyonder .co.uk...[color=green]
              > > 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[/color][/color]
              array[color=blue]
              > of[color=green]
              > > 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[/color]
              > the[color=green]
              > > 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[/color]
              > and[color=green]
              > > 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[/color]
              > and[color=green]
              > > add element to array?
              > > }
              > > ...
              > > } // end of class Cart[/color]
              >
              > In this code, you show an attempt to add an element to $order. But where[/color]
              did[color=blue]
              > $order come from? Where did you initialize it by pulling it back out of[/color]
              the[color=blue]
              > stored SESSION data?
              >
              >[color=green]
              > > Here is some code for cart_process.ph p which registers the Cart object[/color][/color]
              in[color=blue][color=green]
              > > the session and calls the add_item() function.
              > >
              > > ...
              > > if ( !isset( $_SESSION['cart] ) ) {
              > > $cart = new Cart;
              > > // register cart in session
              > > $_SESSION['cart'] = $cart; // correct notation?
              > > }[/color]
              >
              > 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[/color]
              cart[color=blue]
              > in the SESSION array.
              >[color=green]
              > > if ( isset( $_POST['addtobasket'] ) ) { // if form submitted
              > > $_SESSION['basket']->add_item( $_POST['item'], $_POST['qty'] );
              > > }[/color]
              >
              > Was this supposed to be referencing 'cart' instead of 'basket'?
              >[/color]

              YES!
              [color=blue]
              > - Virgil
              >
              >[/color]


              Comment

              Working...