Hi all, I know this is going to be a bit of a longshot as I think this is quite complex and maybe hard to understand. I’m creating a website for a lens-hire firm, on the basket, there is a drop down list for the available hire periods such as 3, 7, 14, 21 and 28 day hire. I’m using a bit of jQuery AJAX to update the basket on change of the dropdown list. The cart is updating but without any totals, prices, etc…the title is still there. However, if I am to refresh the page the new hire period is back with all of the correct prices and totals, not sure what I’m doing wrong here. Please see my below code (there’s a lot of it).
Basket Class
<?php
class ShoppingCart {
protected $items = array();
public function is_empty(){
if(empty($this->items)):
return true;
else:
return false;
endif;
}
//Method for adding an item to the cart...
public function add_items($id, $info){
//Is it already in the cart?
if(isset($this->items[$id])):
//Call the update_item() method:
$this->update_item($id, $this->items[$id]['qty']+1);
else:
//Add the array of info...
$this->items[$id] = $info;
//Add the quantity...
$this->items[$id]['qty'] = 1;
endif;
}
public function update_item($id, $qty){
//Delete if quantity equals 0...
if($qty == 0):
$this->delete_items($id);
elseif(($qty > 0) && ($qty != $this->items[$id]['qty'])):
//Update the quantity...
$this->items[$id]['qty'] = $qty;
endif;
}
public function update($period){
foreach($this->items as $id=>$value):
$this->items[$id]['option'] = $period;
endforeach;
}
//Method for deleteing an item...
public function delete_items($id){
//Confirm this isn't in the cart...
if(isset($this->items[$id])): //Remove the item...
unset($this->items[$id]);
endif;
}
public function count_cart(){
//print_r($this->items);
$i = count($this->items);
return $i;
}
public function cart_total(){
foreach($this->items as $value):
$total += $value['price']*$value['qty'];
endforeach;
return $total;
}
public function viewCart($edit){
//If there are products in session array then display cart...
if($this->count_cart()>0):
echo "<form action=\\"$_SERVER[REQUEST_URI]\\" method=\\"post\\">";
echo "<input type=\\"hidden\\" name=\\"do\\" value=\\"update\\" />";
echo "<table width=\\"100%\\" class=\\"basket\\" cellspacing=\\"0\\" cellpadding=\\"5\\">";
$total = 0;
$cnt = 0;
foreach($this->items as $id=>$value):
$price = mysql_query(sprintf("SELECT products_price, products_timeframe, products_price FROM pricing WHERE pricing_id = %s", $value['option']));
$cost = mysql_fetch_assoc($price);
echo "<tr>";
echo "<td width=\\"78\\"><img src=\\"/images/products/$value[image]\\" alt=\\"$value[name]\\" width=\\"78\\" height=\\"60\\" /></td>";
echo "<td>".$value['name'];
echo '<br/><span class="smallPrice">Price based on a '.$cost['products_timeframe'].' Day Hire</span>';
echo "</td>";
echo "<td>£".number_format($cost['products_price'],2)."</td>";
if($edit===1):
echo "<td><input type=\\"text\\" name=\\"qty[$id]\\" size=\\"3\\" value=\\"$value[qty]\\" class=\\"basketQty\\"/>
</td>";
else:
echo "<td>$value[qty]</td>";
endif;
echo "<td><strong>£".number_format($cost['products_price']*$value['qty'],2)."</strong></td>";
echo "</tr>";
endforeach;
echo "</table></form>";
//If there are no products with a matching session_id, then display message...
else:
echo "<h3>Your basket is empty!</h3>";
endif;
}
function row_color($cnt,$even,$odd) {
echo ($cnt%2) ? "<tr class=\\"$odd\\">" : "<tr class=\\"$even\\">";
}
function grand_total(){
foreach($this->items as $id=>$value):
$price = mysql_query(sprintf("SELECT products_price FROM pricing WHERE pricing_id = %s", $value['option']));
$cost = mysql_fetch_assoc($price);
$multi[] = $cost['products_price']*$value['qty'];
endforeach;
return number_format($total = array_sum($multi),2);
}
}
?>
Script that the AJAX Runs
<?php
session_start();
require('shopping.cart.php');
//Initalize the shopping cart...
if(isset($_SESSION['cart'])):
$bag = unserialize($_SESSION['cart']);
else:
$bag = new ShoppingCart();
endif;
if(isset($_POST['period'])):
$bag->update($_POST['period']);
$_SESSION['cart'] = serialize($bag);
echo $bag->viewCart(1);
else:
echo 'Unsure';
endif;
?>
Appreciate this maybe hard to understand, I know it’s a bit of a longshot - I also need to tidy up by tableHeader class and return a variable instead of so many echos!