Link to home
Start Free TrialLog in
Avatar of abstractionz
abstractionz

asked on

Problem with Sessions

I am creating a shopping cart and am having problems with showing the cart -- when the update button is clicked to change the quantity, the quantity that is displayed doesn't change:

require_once('Cart.php');
require_once('Product.php');
require_once('LineItem.php');
require_once('functions.php');
session_start();

if($_POST['submit_check']){ // the user has clicked the update button -- the cart already exists
      // get the product code
      // get the new quantity
      // get the existing cart from the existing session
                // then if the submitted product code matches an existing product code
                // update the quantity for that product in the session
      // then display the cart again

                // i've tested this portion of my code and it updates the quantity correctly
}else
{
    // a cart has not yet been created so create, place it in the session, then display it
    // display cart
    // this portion of my code also works correctly
}

i think the problem is in my display cart function?:

function display(){
print '<table><tr><th>Quantity</th><th>Description</th><th>Price</th><th>Amount</th>';
$cart = $_SESSION['cart'];
$items = $cart->getItems();
foreach($items as $value){
      $product = $value->getProduct();
      print '<tr>' .
                  '<td><p>' .
                    '<form action="showcart.php" method="post">' .
                        '<input type="hidden" name="productCode" value="' . $product->getCode() . '">' .
                        '<input type="hidden" name="submit_check" value="1">' .
                        '<input type="text" size="2" name="quantity" value="' . $value->getQuantity() . '">' .
                        '<input type="submit" value="Update">' .
                    '</form>' .
                  '</td>' .
                  '<td><p>' . $product->getDescription() . '</td>' .
                  '<td><p>' . $product->getPrice() . '</td>' .
                  '<td><p>' . $value->getTotalPrice() . '</td>' .
               '</tr>';
      }
}

It all seems to logically make sense.  I'm not sure why the quantity that is displayed when updated is the old quantity, not the new quantity, even though the new quantity is in the session cart.  Am I using sessions incorrectly?
Avatar of d_tan
d_tan

display cart function looks ok to me. . .

since the cart is being retrieved from the session ok, i would probably assume that the product quantity is not getting updated properly.

Try using print_r($_SESSION) after you update the cart. . .that'll print out all the values for the session array.  

This is php 4?? php 5??  If its php 4, remember that if you are updating the quantity with objects, you might need to pass by reference.

Otherwise, maybe post the code for the update part??

dtan
Avatar of abstractionz

ASKER

This is PHP 4.3

I used print_r($_SESSION) and it showed that the quantity is not being updated.  Here is my code for the update:



if($_POST['submit_check']){
      $code = $_POST['productCode'];
      $quantity = $_POST['quantity'];
      $cart = $_SESSION['cart'];
      $items = $cart->getItems();
      foreach($items as $value){
            $product = $value->getProduct();
            if($product->getCode() == $code){
                  $value->setQuantity($quantity);
            }
      }
      display(); // this is the function from my previous post
HI again,

try restoring the cart back into the session. . .  

$_SESSION['cart'] = $cart;

Make sure that the update is actually occurring in the foreach and cart objects too.  (use print_r)
I'm so used to php 5 now. . .but the call to cart->getItems() may return a copy of the items, vs the items themselves.

If this is the case you may need to do something along the lines of $items = &$cart->getItems();  But again, I'll leave this to you to double check.

The object model in php 4 is such that it makes copies of objects everywhere. . .  you have to make sure that if you want the acual reference to the object, you use the & . . .

dtan
Sorry . . .little bit of a typo:

Make sure that the update is actually occurring in the foreach loop and cart object too.  Eg.  Before and after the foreach loop, print_r($items) and print_r($carts).  This will give you a great deal of insight to how the php 4 model works. . .

dtan
I found the problem.  In the foreach loop first I find the item that needs its quantity changed like I originally had, then I make a copy of that item, remove it, then add the temporary copy with the new quantity.

if($_POST['submit_check']){
      $code = $_POST['productCode'];
      $quantity = $_POST['quantity'];
      $cart = $_SESSION['cart'];
      $items = $cart->getItems();
      print_r($items);
      foreach($items as $value){
            $product = $value->getProduct();
            if($product->getCode() == $code){
                  $value->setQuantity($quantity); // change the local value
                  $temp = $value; // make a temp copy of it
                  $cart->removeItem($value); // remove the old one
                  $cart->addItem($temp); // add the new one
            }
      }
      
      $_SESSION['cart'] = $cart;
      display();
}else{

I'm getting PHP 5 ASAP  :)
Careful though =p  Not all webservers will have php 5 installed. . .

dtan
it won't matter whether it is php 5 or 4...
the problem is that
$cart is not the same as $_SESSION['cart']
it just has the same values...
try $cart = &$_SESSION['cart']
actually in php5 the object model is completely different, so in php5, $cart would reference $_SESSION['cart'] while in php4 $cart would be a copy.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial