Place an Array in the Session. Add the items to the array.
$_SESSION['cart'] = array();
$_SESSION['cart'][] = $apples;
$_SESSION['cart'][] = $oranges;
$_SESSION['cart'][] = $pears;
Note: replace $apples
, $oranges
and $pears
with your product ids.
You access the array like any other array in PHP, e.g. to count the items:
echo count($_SESSION['cart']);
and to iterate over the items:
foreach($_SESSION['cart'] as $item)
{
echo $item;
}
You could also wrap the Session into an object and provide access to the cart through a method interface, but I leave that for someone else to explain.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…