2016-05-13 34 views
2

我有一個問題,當我嘗試將購物車添加到購物車。購物車無法進入購物車。Codeigniter - 爲什麼加入購物車不起作用?

這是我的控制器:

public function buy($product_id) 
    { 
     $product = $this->m_produk->find($product_id); 
     $data = array(
         'id'  => $product->id, 
         'qty'  => 1, 
         'nama' => $product->nama, 
         'harga' => $product->harga 
         ); 

     $this->cart->insert($data); 
     redirect('produk/index'); 
     } 

這是我的模型加入購物車:

public function find($id){ 
    $hasil = $this->db->where('id',$id) 
         ->limit(1) 
         ->get('produk'); 
    if ($hasil->num_rows() > 0) { 
     return $hasil->row(); 
    }else { 
     return array(); 
    } 
} 

這是我的看法:

<?php 

foreach ($produk as $product) { 

?> 
<div class="thumbnail"> 
<img src="<?php echo base_url(''.$product->gambar);?>" width="200" height="200" class="img-rounded center-block" alt=""/> 
<div class="caption"> 
<h4 class="text-center"><?php echo $product->nama?></h4> 
<h4 class="text-center">RP.<?php echo $product->harga?>,00</h4> 
<!-- <a href="#" class="link-class btn btn-primary center-block" role="button">add to cart</a> --> 
<?=anchor('produk/buy/'.$product->id,'add to cart' , [ 
    'class' => 'btn btn-primary' , 'role' => 'button' 
]) ?> 
</div> 
</div> 
<?php } ?> 
</div> 

PS:當我print_r我的內容,購物車只顯示:array()其手段當我添加購物車,購物車不能進入購物大車。

任何人都可以解決這個問題嗎?

謝謝。

+0

可以給我解決這個問題嗎? –

+0

你的'insert()'函數看起來像什麼? – Marius

+0

@marius我不明白你的意思,你可以具體說明insert()函數嗎? –

回答

0

按照文檔已簡稱: https://codeigniter.com/userguide3/libraries/cart.html

重要:上述(ID,數量,價格,和名稱)前四個數組索引是必需的。如果您忽略其中的任何一項,則數據將不會保存到購物車。第五個索引(選項)是可選的。它旨在用於您的產品具有與之相關聯的選項的情況。如上所示,使用數組作爲選項。

請通過所有必需的參數。 你有name拼寫錯誤或丟失,price失蹤

+0

其工作,你拯救了我的生命。非常感謝你 –