2017-04-25 30 views
2

我有這樣的控制器:車塊不更新笨

<?php 
 
class Cart extends CI_Controller{ 
 
    public $paypal_data = ''; 
 
    public $tax; 
 
    public $shipping; 
 
    public $total = 0; 
 
    public $grand_total; 
 

 
    /* 
 
    *Cart Index 
 
    */ 
 
    public function index(){ 
 
    //Load view 
 
    $data['main_content'] = 'cart'; 
 
    $this->load->view('layouts/main', $data); 
 
    } 
 

 
    /* 
 
    *Add To Cart 
 
    */ 
 
    public function add(){ 
 
    //Item data 
 
    $data = array(
 
     'id'=> $this->input->post('item_number'), 
 
     'qty'=> $this->input->post('qty'), 
 
     'price'=> $this->input->post('price'), 
 
     'name'=> $this->input->post('title'), 
 
    ); 
 
    //print_r($data);die(); 
 

 
    //Insert Into cart-block 
 
    $this->cart->insert($data); 
 

 
    redirect('products'); 
 
    } 
 
    }

我有使用這種控制器的視圖文件:

<div class="cart-block"> 
 
    <form action="cart/update" method="post"> 
 
    <table cellpadding="6" cellspacing="1" style="width:100%" border="0"> 
 
     <tr> 
 
     <th>QTY</th> 
 
     <th>Item Description</th> 
 
     <th style="text-align:right">Item Price</th> 
 
     </tr> 
 
     <?php $i = 1; ?> 
 
     <?php foreach ($this->cart->contents() as $items) : ?> 
 
     <input type="hidden" name="<?php echo $i. '[rowid]'; ?>" value="<?php echo $items['rowid']; ?>" /> 
 
     <tr> 
 
      <td><input type="text" name="<?php echo $i.'[qty]'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" size= "5"</td> 
 
      <td><?php echo $items['name']; ?></td> 
 
      <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td> 
 
     </tr> 
 
     <?php $i++; ?> 
 
     <?php endforeach; ?> 
 
     <tr> 
 
     <td></td> 
 
     <td class="right"><strong>Total</strong></td> 
 
     <td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td> 
 
     </tr> 
 
    </table> 
 
    <br> 
 
    <p><button class="btn btn-default" type="submit">Update Cart</button> 
 
    <a class="btn btn-default" href="cart">Go To Cart</a></p> 
 
    </form> 
 
</div> 
 
<div class="panel panel-default panel-list"> 
 
    <div class="panel-heading panel-jeading-dark"> 
 
    <h3 class="panel-title"> 
 
     Categories 
 
    </h3> 
 
    </div> 
 

 
    <!--List group --> 
 
    <ul class="list-group"> 
 
    <?php foreach(get_categories_h() as $category) : ?> 
 
    <li class="list-group-item"><a href="<?php echo base_url(); ?>products/category/<?php echo $category->id; ?>"><?php echo $category->name; ?></a></li> 
 
    <?php endforeach; ?> 
 
    </ul>

我道歉我無法向您展示實際的網站,因爲它不在線,但基於上述片段可以指出視圖文件或控制器中導致購物車不更新的錯誤?

我在看一個視頻教程,但似乎我做錯了什麼。這裏的視頻:https://youtu.be/ajt_DJMS5FM?t=13m45s

+0

車庫您沒有** **更新方法**購物車**控制器,你的意思是不加入? –

回答

0

你的表單動作是購物車/更新你的控制器中的功能是添加?

將表單動作更改爲購物車/添加。或者寫一個購物車/更新功能。

+0

我嘗試過,但仍然沒有運氣。 –

+0

跳過這裏https://youtu.be/ajt_DJMS5FM?t=925它解釋了控制器功能。 – Ryan

+0

是的,我現在一遍又一遍地看着它,這就是爲什麼它沒有意義。我還添加了購物車更新功能,但受同一問題影響。 –

0

窗體動作首先改變行動= 「購物車/更新」

action="<?php echo base_url();?>cart/add" and apply die(); and test it 
+0

我在哪裏申請die(); ? –

+0

你的控制器添加()函數.. –

+0

好吧,所以我申請了die();在數組之後,這是正確的嗎? –

0

負載控制器

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

沒有運氣,謝謝。 –