Solved

Problem with Sessions

Posted on 2004-08-02
10
190 Views
Last Modified: 2006-11-17
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?
0
Comment
Question by:abstractionz
10 Comments
 
LVL 2

Expert Comment

by:d_tan
ID: 11699744
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
0
 

Author Comment

by:abstractionz
ID: 11699828
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
0
 
LVL 2

Expert Comment

by:d_tan
ID: 11699918
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
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 2

Expert Comment

by:d_tan
ID: 11699930
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
0
 

Author Comment

by:abstractionz
ID: 11700133
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  :)
0
 
LVL 2

Expert Comment

by:d_tan
ID: 11700144
Careful though =p  Not all webservers will have php 5 installed. . .

dtan
0
 
LVL 20

Expert Comment

by:virmaior
ID: 12020511
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']
0
 

Author Comment

by:abstractionz
ID: 12020581
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.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12653438
PAQed with points refunded (300)

modulo
Community Support Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question