[eluser]akho[/eluser]
Hi, I'm new with Codeigniter. I'm try to make a shopping cart but when i'm update shopping cart it doesn't work. someone please help me
here is my model
Code:
<?php
class MOrders extends CI_Model{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function updateCart($productid,$fullproduct){
//pull in existing cart first!
$cart = $_SESSION['cart'];
$totalprice = 0;
if (count($fullproduct)){
if (isset($cart[$productid])){
$prevct = $cart[$productid]['count'];
$prevname = $cart[$productid]['name'];
$prevprice = $cart[$productid]['price'];
$cart[$productid] = array(
'name' => $prevname,
'price' => $prevprice,
'count' => $prevct + 1
);
}else{
$cart[$productid] = array(
'name' => $fullproduct['name'],
'price' => $fullproduct['price'],
'count' => 1
);
}
foreach ($cart as $id => $product){
$totalprice += $product['price'] * $product['count'];
}
$_SESSION['totalprice'] = $totalprice;
$_SESSION['cart'] = $cart;
$this-> session-> set_flashdata('conf_msg', "We've added this product to your cart.");
}
}
function updateCartAjax($idlist){
$cart = $_SESSION['cart'];
//split idlist on comma first
$records = explode(',',$idlist);
$updated = 0;
$totalprice = $_SESSION['totalprice'];
if (count($records)){
foreach ($records as $record){
if (strlen($record)){
//split each record on colon
$fields = explode(":",$record);
$id = $fields[0];
$ct = $fields[1];
if ($ct > 0 && $ct != $cart[$id]['count']){
$cart[$id]['count'] = $ct;
$updated++;
}elseif ($ct == 0){
unset($cart[$id]);
$updated++;
}
}
}
if ($updated){
$totalprice =0; //with changes, must reset this value!
foreach ($cart as $id => $product){
$totalprice += $product['price'] * $product['count'];
}
$_SESSION['totalprice'] = $totalprice;
$_SESSION['cart'] =$cart;
echo $updated . "records updated!";
}else{
echo "No changes detected!";
}
}else{
echo "No records to update!";
}
}
function removeLineItem($id){
$totalprice = 0;
$cart = $_SESSION['cart'];
if (isset($cart[$id])){
unset($cart[$id]);
foreach ($cart as $id => $product){
$totalprice += $product['price'] * $product['count'];
}
$_SESSION['totalprice'] = $totalprice;
$_SESSION['cart'] = $cart;
echo "Product removed.";
}else{
echo "Product not in cart!";
}
}
}
?>