简体   繁体   English

如何在Codeigniter购物车中更新多行

[英]How to update multiple rows in codeigniter cart

I am using codeigniter cart to store multiple rows. 我正在使用codeigniter购物车来存储多行。 This is a booking system site. 这是一个预订系统站点。

here's my code to retrieve the cart: 这是我检索购物车的代码:

<?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; ?>

The clear_cart() function works fine with the code below: 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>

And this is the remove function in my controller: 这是我控制器中的remove函数:

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

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

    redirect('bookings');
}

But I don't know how to update the cart if for example I have more than 1 row. 但是,例如,如果我有多于1行,我不知道如何更新购物车。 The important here is to update the number of rooms (ie qty) in the cart. 这里重要的是更新购物车中的房间数量(即数量)。

You can use multi-dimensional array 您可以使用多维数组

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM