Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with Sessions

Posted on 2004-08-02
10
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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