2012-08-17 39 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);