views:

73

answers:

3

Hi, I made an PHP OOP Cart Class like this

print_r($_SESSION["cart"]);

the result is:

Array ( [1] => 1 [3] => 2 )

How can I print this session, like a "real" cart? example:

echo "Your basket: ";
echo "ItemID: ".$cartid." Itemnumber:".$cartnumber;

The adding to basket part:

if (isset($_POST['submit'])) 
{ 
$cart= new Cart(); 
$cart->add($_POST['id'],2); 
$item= $cart->getCart(); 
$_SESSION["cart"]=$cart;
}
A: 
<?php

function printCart($cartid, $cartnumber) {
    echo "ItemID: ".$cartid." Itemnumber:".$cartnumber;
    echo "<br />";
}

$a = array(1 => 1, 3 => 2);

array_walk($a, 'printCart');

See array_walk.

Artefacto
A: 

The way the code in that tutorial is setup is that the array index is the item ID and the value is the quantity, so something like this should do it:

echo 'Your basket:<br>';
foreach ($_SESSION['cart'] as $id => $quantity) {
    echo 'ItemID:' . $id . ' Itemnumber: ' . $quantity . '<br>';
}
Daniel Egeberg
I tried this solution, but this displayed only the newest id
Dodi
That's because you're overwriting the previously stored info in $_SESSION. It seems that you don't understand how to use that cart class. From my quick read of that linked page, the info stored in $_SESSION is just an array. The cart class is used to manage that array.
George Marian
A: 
  1. $_SESSION is just an array.
  2. An array is being stored in $_SESSION['cart'].
  3. You seem to be overwriting that array, with only the newly added item.

As an example, this code:

if (isset($_POST['submit'])) 
{ 
$cart= new Cart(); 
$cart->add($_POST['id'],2); 
$item= $cart->getCart(); 
$_SESSION["cart"]=$cart;
}

can be changed to:

if (isset($_POST['submit'])) 
{ 
$_SESSION["cart"][$_POST['id']] = 2;
}

Now, it'll simply add to the array stored in $_SESSION['cart']. Notice, that it doesn't use the cart at all.

Try:

if (isset($_POST['submit'])) 
{ 
$cart= new Cart($_SESSION["cart"]); // this should bring in the previous items
$cart->add($_POST['id'],2); // what is this magic number '2'? Quantity?  But where is it from?
$_SESSION["cart"] = $cart->getCart();
}

Edit: Some more explanation.

The cart class' constructor accepts a parameter that it stores internally:

private $cart;

function __construct($cart="") {
    $this->cart = $cart;
}

In all honestly, this should be a bit more intelligent:

private $cart;

function __construct($cart = array()) {
    if (is_array($cart))
    {
         $this->cart = $cart;
    }
    else
    {
         // maybe print some error, informing the developer that he's using the cart class incorrectly
         // or better yet, trigger a PHP warning:
         trigger_error('Cart class constructor expects an array parameter',      E_USER_WARNING);
    }
}

This change guards against providing anything but an array to the constructor. Note, that it can still be given an array that the class doesn't understand, e.g. a multidimensional array. In that situation, the cart class won't destroy existing data in the array, unless an item id matches one of the keys in the original array.

George Marian
Apparently, some people don't like this answer. It'd be nice to know why they feel that way.
George Marian