Solved

Problem with Sessions

Posted on 2004-08-02
10
187 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now