Hi,
I am trying to add a number of items to a session variable by a shopping cart class, but it doesn’t seem to be working. My code in steps is shown below:
Cart.class.php
<?php
class Cart {
public function AddToCart(){
!isset($_SESSION['ID']) ? $_SESSION['ID'] = array() : '';
!isset($_SESSION['theName']) ? $_SESSION['theName'] = array() : '';
!isset($_SESSION['quantity']) ? $_SESSION['quantity'] = array() : '';
!isset($_SESSION['price']) ? $_SESSION['price'] = array() : '';
!isset($_SESSION['image']) ? $_SESSION['image'] = array() : '';
array_push($_SESSION['ID'], $_POST['ID']);
array_push($_SESSION['theName'], $_POST['theName']);
array_push($_SESSION['quantity'], $_POST['quantity']);
array_push($_SESSION['price'], $_POST['price']);
array_push($_SESSION['image'], $_POST['image']);
$loc = $_SERVER['HTTP_REFERER'];
echo "<script>window.location.href='".$loc."'</script>";
}
public function getTotalPrice(){
$i = 0;
$total = 0;
if(isset($_SESSION['quantity'])){
foreach($_SESSION['price'] as $price){
while(!isset($_SESSION['price'][$i]) && !isset($_SESSION['quantity'][$i])){
$i++;
}
$total = $total + ($price * $_SESSION['quantity'][$i]);
$i++;
}
}
return $total;
}
public function getTotalItems(){
$total = 0;
if(isset($_SESSION['quantity'])){
foreach($_SESSION['quantity'] as $quantity){
$total = $total + $quantity;
}
}
return $total;
}
public function emptyCart(){
session_destroy();
}
}
Then in my controller, ‘view-item.php’ i have the following:
<?php
require_once('Models/Cart.class.php');
$cart = new Cart('localhost', 'root', '', 'test');
if(isset($_POST['ID'])){
$cart->AddToCart();
}
require_once('Views/view-item.phtml');
Then in my view, i use a form to add to the session with hidden values like so:
<form method="post" action="">
<h2><?php echo $row['theName']; ?></h2>
<input type="hidden" name="ID" value="<?php echo $row['theID']; ?>" />
<input type="hidden" name="theName" value="<?php echo $row['theName']; ?>" />
<input type="hidden" name="price" value="<?php echo $row['price']; ?>" />
<input type="hidden" name="image" value="<?php echo $row['image']; ?>" />
<input type="hidden" name="quantity" value="1" />
<div id="description" class="right">
<p><?php echo $row['description']; ?></p><br/>
<span>£ <?php echo $row['price']; ?></span><br/>
<input type="submit" name="add-to-basket" id="add-to-basket" class="add-to-basket" value=""/>
</div>
<div id="view-items-box">
<?php echo $str; ?>
<a href="items/<?php echo $row['image']; ?>" class="jqzoom" title="<?php echo $row['theName']; ?>" id='mainImage'>
<img src="items/<?php echo $row['image']; ?>" alt="" title="<?php echo $row['theName']; ?>" id='mainImage' class="item-image-large">
</a>
<div id="thumbnails">
<a href="items/<?php echo $row['image']; ?>"><img src="items/<?php echo $row['image']; ?>" alt="" title="<?php echo $row['image']; ?>" class="item-image-small" onMouseOver='loadImg(0)'/></a>
<a href="items/<?php echo $row['image_1']; ?>"><img src="items/<?php echo $row['image_1']; ?>" alt="" title="<?php echo $row['image_1']; ?>" class="item-image-small" onMouseOver='loadImg(1)'/></a>
<a href="items/<?php echo $row['image_2']; ?>"><img src="items/<?php echo $row['image_2']; ?>" alt="" title="<?php echo $row['image_2']; ?>" class="item-image-small" onMouseOver='loadImg(2)'/></a>
</div>
</div>
</form>
$row[‘’] is just the rows from a database.
Now when i click on the button why doesn’t the session work? I test the session variable to check what is in the shopping cart like this:
<?php
require_once('Models/Cart.class.php');
$basket = new Cart('localhost', 'root', '', 'test');
$bag = $basket->getTotalItems();
$price = $basket->getTotalPrice();
session_start();
?>
<div id="shopping-bag">
<div id="shopping-bag-contents">
<div class="padding">
<span onmouseover="shoppingCartExpand()">
<span style="float:left; margin-top:8px;">
Shopping bag:
<strong>
<span style="margin: 0 20px 0 10px;">
Items: <?php echo $bag; ?>
</span> Total: £<?php echo $price; ?>
</strong>
</span>
<img src="images/basket2.png" alt="basket" style="float:right; margin:3px 10px;" />
</span>
<br/><br/>
<ul id="shopping-list" style="margin:10px 0 0 20px;padding:0;">
<?php
if(!empty($_SESSION['theName'])){
$i=0;
foreach($_SESSION['theName'] as $name){
while(!isset($_SESSION['price'][$i]) && !isset($_SESSION['quantity'][$i])){
$i++;
}
echo "<li>".$name." - £". $_SESSION['price'][$i]." x".$_SESSION['quantity'][$i]." - <small><a href=\\"".SITEADDRESS."remove-item/$i\\">[remove]</a></small></li>";
$i++;
}
}else{
echo "<li>Basket is empty</li>";
}
?>
</ul>
<br/>
<div class="close" style="width:100px; float:left;">
<a onmouseover="JavaScript:this.style.cursor='hand';" onclick="shoppingCartShrink()" href="#">[x] close</a>
</div>
<span style="float:right; margin-right:10px;">
<a onclick="return confirm('Are you sure? This will remove all items from your basket')" href="<?=SITEADDRESS?>empty-session">Empty basket</a> |
<a href="">Checkout</a>
</span>
</div>
</div>
</div>
So if you look at <?php echo $bag; ?> and <?php echo $price; ?>
I am trying to display the contents of the shopping basket.
But my basket is always showing as empty, can anyone please help me find out how i can fix this?
Thanks
I would really appreciate if someone can advise me as to what i need to do?
Thanks again