I'm trying to write a simple cart class and wondering how to use sessions to preserve the cart values.
Can I change session variables by reference to the instance, or should I just use $_SESSION for everything in my class?
That doesn't seem to look quite as good because I'm setting some variables for messages on every page... but nothing is stored in the session...
Guess I could really use a primer on using sessions within OOP! Thanks
class shoppingCart
{
var $messages = array();
var $errors = array();
var $currentUserId = false;
var $dbServer = "";
var $dbUser = "";
var $dbPass = "";
var $dbName = "";
function __construct()
{
$this->dbConnect();
$this->checkLogin();
if(isset($_POST['cart'])) {
$this->doCart();
}
if(isset($_POST['user'])) {
$this->doUser($_POST['user']);
}
}
function checkLogin()
{
session_start();
}
function doCart()
{
$this->catalog = $this->getCatalog();
foreach($this->catalog as $item => $details)
{
if(array_key_exists($item, $_POST)) {
$this->cart[$item]['quantity'] = (int)$this->cart[$item]['quantity'] + $_POST[$item];
if($_POST[$item] == 0) {
unset($this->cart[$item]);
}
$total += $this->cart[$item]['quantity'] * $this->catalog[$item]['price'];
}
}
$_SESSION['cart'] = (array)$this->cart;
$_SESSION['cartTotal'] = (int)$this->cartTotal = $total;
#### Why am I storing these twice here?? Which do I use on page x?
}