Solved

Problem with Sessions

Posted on 2004-08-02
10
193 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

636 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