2015-08-27 114 views
2

我正在嘗試更新我的購物車(通過客戶更改銷售產品的數量),並使用購物車庫。當我提交更新按鈕時,頁面將按照我的意願重定向,但不會更新並顯示以前的值,並且我不會收到任何錯誤。我的代碼有什麼問題?在Codeigniter v2.2中更新購物車庫

我的更新控制器代碼

public function update($in_cart = null){ 
     $data= $_POST;  
     $this->cart->update($data);   

     //show cart page 
     redirect ('cart','refresh');  
    } 

和我的視圖代碼是

<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; ?> <!-- $i=1 is a counter --> 
      <?php foreach ($this->cart->contents() as $items) : ?> 
      <input type="hidden" name="<?php echo $i.'[rowid]'; ?>" value="<?php echo $items['rowid'];?> "/> 
      <tr> 
       <td id="cart-qty"><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> 

回答

0

嘗試修改您的控制器功能:

public function update(){ 
    $rowid=$this->input->post('rowid'); 
    $cart=$this->cart->contents(); 
    foreach ($cart as $cart) { 
    //now match your item whose qty is updated 
    if($rowid==$cart['rowid']){ 
    $qty=$cart['qty']; 
    } 
    } 
    $data=array(
    'rowid'=>$rowid, 
    'qty'=>$qty+1 
); 
    $data=$this->cart->update($data); 
    redirect ('cart','refresh');  
} 
+0

我只是想你的代碼,但沒有奏效。我的代碼有兩個錯誤 – Mehdi

+0

嘗試編輯我的新代碼。 –

+0

仍然與他們有一個錯誤,我想我自己的控制器應該是對的,因爲我沒有任何錯誤,可能這個問題是從我的角度來看的。 – Mehdi

0

在更新一般我們使用id在這種情況下更新任何行我們還需要一個id,因此在購物車庫中添加它會創建一個自動row_id的e非常的產品,所以你必須使用ROW_ID更新內容車請按照下面的圖像 -

enter image description here

+0

我從Udemy教程視頻中看到了我的代碼,並且我已經根據視頻完成了他們的編寫,在視頻中,教師在我們使用{$ data = $ _POST;}的視頻中說過:我們將所有後期數據轉換爲$數據。 – Mehdi

+0

好的,但你必須嘗試這個... –

+0

我不知道如何寫另一種方式。 qty和row_id的後期價值是多少?你能寫下我的看法嗎? – Mehdi

相關問題