Welcome Guest, Not a member yet? Register   Sign In
cart Shopping
#1

[eluser]elombashy[/eluser]
hiii..

I built a website with a codeigniter shopping cart and ajax, and the problem is when i press the add-button, sometimes it adds the items, sometimes it replaces it and sometimes it removes it!
here is my code:

//html:
<a
class="add_to_cart_btn"
href="main/add_to_cart/&lt;?php echo $categoriesAndProdcuts[$key][$i]['productId']; ?&gt;
">

//jquery:
$('.add_to_cart_btn').click(function(){

href=$(this).attr('href');
href_array = href.split('/');
id=href_array[2];

$.ajax({
type: "post",
cache: false,
url: href,
data: '',
success: function(data){
if(data){
$("#main_cart").remove();
$("#mostVisited").after(data);
}
}
});

$.ajax({
type: "post",
cache: false,
url: 'main/total_cart_products_plus',
data: '',
success: function(data){
$("#total_cart_products").html(data);
}
});
return false;
});


//Controller:
function add_to_cart($id) {

$product = $this->cart_m->get($id);
$products=$this->cart->contents();
$ids=array();
foreach ($products AS $k=>$v){

$ids[$k]=$v['id'];
}
if(in_array($id, $ids)){
$rowid = array_search($id, $ids);
$update_data=array(
'rowid'=> $rowid,
'qty' => $v['qty']+1
);
$this->cart->update($update_data);
}
else{
$data = array(
'id' => $id,
'name' => $product[0]['name'],
'qty' => 1,
'price' => $product[0]['price'],
'image' => $product[0]['image']
);
$this->cart->insert($data);

}
$this->load->view('cart/main_cart');
}

//Model:
function get($id) {
$data=array();
$this->db->select('name, price, image')->from('product')
->join('product_description','product.product_id=product_description.product_id')
->where('product.product_id',$id)->where('language_id',2);
$results = $this->db->get();
//var_dump($results);die();
if($results->num_rows()>0){
foreach($results->result_array() AS $result){
$data[]=$result;
}

}
return $data;
}


Any Suggestions??
#2

[eluser]toopay[/eluser]
I hope codeigniter mods apply...
Code:
if(trim($post) == '') die ('You must write something!');




Theme © iAndrew 2016 - Forum software by © MyBB