2015-06-25 33 views
0

我想在codeigniter中創建購物車。任何人都可以告訴我用數據庫和PHP會話做這件事的最好方法。php會話和數據庫在codiegniter

我通過互聯網發現了很多東西,但沒有解決我的問題。

請給我最簡單的方法。

我是codeigniter的新手。

+0

爲什麼建立自己的購物車,而我們有很大的資源在互聯網上,就像opencart等 –

+0

查看下面的答案帖子 –

+0

客戶需要網站只在codeigniter。 –

回答

1
CodeIgniter provides a default `Cart` class for these purposes. 

你只需如果您使用的是CodeIgniter'sCart類是強制性的,你給(id, qty, price, and name)每個產品加載庫

$this->load->library('cart'); 

購物車類提供了方法insertupdate的項目。

這裏是車類

CodeIgniter's DOCS這是Tutorial,幫助我掌握如何使用購物車類,我用

+0

謝謝,我正在尋找這樣的東西。 –

+0

總是一種享受:) – Abhinav

1

樣車的代碼。使用您自己的購物車選擇用這些代碼的幫助

控制器

public function __construct() 
    { 
     parent::__construct(); 
     $this->load->library('session');//session library 
     $this->load->model('Model_Name');//model library 
     $this->load->library('cart');//cart library 
    } 

插入數據到車

在購物車idqtypricename字段必須

public function insert_cart() 
    {   
     $data = array(
      'id' => $this->input->post('id'), 
      'qty' => $this->input->post('qty'), 
      'price' => $this->input->post('price'), 
      'name' => $this->input->post('name'), 
     ); 
     $this->cart->product_name_rules = '[:print:]'; 
     $this->cart->insert($data); 
    } 

刪除數據,以車

function remove($rowid) 
    { 
     if ($rowid==="all") 
     { 
      $this->cart->destroy(); 
      redirect('index.php/main');//relevent page 
     } 
     else 
     { 
      $data = array(
       'rowid' => $rowid, 
       'qty'  => 0 
      ); 
      $this->cart->update($data); 
     } 

     // This will show cancle data in cart. 
     redirect('index.php/cart'); 
    } 

更新數據,以車

function update_cart() 
    { 

     $cart_info = $_POST['cart'] ; 

     foreach($cart_info as $id => $cart) 
     { 
      $rowid = $cart['rowid']; 
      $qty = $cart['qty']; 
      $price = $cart['price']; 

      $data = array(
       'rowid' => $rowid, 
       'qty'  => $qty, 
       'price'=> $price, 
      ); 
      $this->cart->update($data); 
      $this->cart->contents(); 
     } 
     redirect('index.php/cart'); 
    } 

CodeIgniter Shopping Cart

Example from Google

+0

謝謝,阿卜杜拉我正在嘗試這裏給出的答案。 –

+0

好的。享受編碼:) –