2012-08-17 33 views
0

我使用codeigniter購物車來存儲多行。這是預訂系統網站。如何更新codeigniter購物車中的多行

這裏是我的代碼來檢索購物車:

<?php if ($cart = $this->cart->contents()): ?> 
<table class="booking"> 
    <tr> 
    <td>#</td> 
    <td>Room Type</td> 
    <td>Check In</td> 
    <td>Checkout</td> 
    <td>Nights</td> 
    <td align="right">Price/Night</td> 
    <td width="10">Rooms</td> 
    <td align="right">Amount</td> 
    <td>Options</td> 
    </tr> 
    <?php $grand_total = 0; $i = 0; ?> 
    <?php foreach ($cart as $item): ?> 
    <tr bgcolor="#FFFFFF"> 
    <td><?php echo $i+1; ?></td> 
    <td><?php echo $item['name']; ?></td> 
    <td><?php echo $item['checkin']; ?></td> 
    <td><?php echo $item['checkout']; ?></td> 
    <td align="center"><?php echo $item['nights']; ?></td> 
    <td align="right"><?php echo number_format($item['subtotal'],2); ?></td> 
    <td><input type="text" name="booking<?php echo $item['id'] ?>" value="<?php echo $item['qty'] ?>" maxlength="3" size="1" style="text-align: right" /></td> 
    <?php $amount = $item['subtotal'] * $item['qty']; ?> 
    <?php $grand_total = $grand_total + $amount; ?> 
    <td align="right"><?php echo number_format($amount,2) ?></td> 
    <td><?php echo anchor('bookings/remove/'.$item['rowid'],'Cancel'); ?></td> 
    <?php endforeach; ?> 
    </tr> 
    <tr> 
    <td colspan="2"><b>Order Total: <?php echo number_format($grand_total,2); ?></b></td> 
    <td colspan="7" align="right"> 
    <input type="button" value="Clear Cart" onclick="clear_cart()"> 
    <input type="button" value="Update Cart" onclick="update_cart()"> 
     <?php 
     /*if(isset($_SESSION['sess_user_id']) || (trim($_SESSION['sess_user_id']) <> '')) { 
      $location='booking_checkout.php'; 
     }else{ 
      $location='billing_info.php'; 
     }*/ 
     ?> 
    <input type="button" value="Place Order" onclick="window.location='<?php //echo $location; ?>'"></td> 
    </tr> 
</table> 
<?php endif; ?> 

clear_cart()功能正常工作與下面的代碼:

<script> 
function clear_cart() { 
    var result = confirm('Are you sure want to clear all bookings?'); 

    if(result) { 
     window.location = "http://localhost/reservation/bookings/remove/all"; 
    }else{ 
     return false; // cancel button 
    } 
} 
</script> 

而且這是在我的控制器的刪除功能:

function remove($rowid) { 
    if ($rowid=="all"){ 
     $this->cart->destroy(); 
    }else{ 
     $data = array(
      'rowid' => $rowid, 
      'qty'  => 0 
     ); 

     $this->cart->update($data); 
    } 

    redirect('bookings'); 
} 

但我不知道如何更新購物車,例如,如果我有mor e比1排。這裏重要的是更新購物車中的房間數量(即數量)。

+0

[你嘗試過什麼?](http://whathaveyoutried.com) – orourkek 2012-08-17 21:48:05

+0

我相信你可以在上面看到我都試過了。大聲笑 – jaypabs 2012-08-18 00:08:16

+0

你沒有發佈任何代碼,試圖做你所要求的,所以它看起來像你說*「我知道如何做一行,你可以給我多行代碼? 「* – orourkek 2012-08-18 00:14:15

回答

1

您可以使用多維數組

$data = array(
    array(
      'rowid' => 'b99c', 
      'qty'  => 3 
     ), 
    array(
      'rowid' => 'xw82', 
      'qty'  => 4 
     ), 
    array(
      'rowid' => 'fh4k', 
      'qty'  => 2 
     ) 
); 

$this->cart->update($data);