代码点火器更新购物车 - 如何为每个 indise 数组


Codeigniter Update cart - how to foreach indise array?

>我在代码点火器更新购物车中遇到问题这是我的观点车。

<script>
	function update_cart(){
		document.forms["updateCart"].submit();
	} 
</script>
<h1>DAFTAR PRODUCT</h1>
<table style="width:100%" border="1">
  <tr style="background-color: yellow;">
    <td>Name</td>
    <td>Price</td>
	<td>Aksi</td>
  </tr>
  <?php foreach ($products as $data):?>
  <tr>
    <td><?php echo $data->name;?></td>
    <td><?php $hargapro = number_format("$data->price",0,",",".");echo "Rp.".$hargapro; ?></td>
	<td><a href="<?php echo base_url();?>cart/add?id=<?php echo $data->id;?>"><button type="button">Add to Cart</button></a></td>
  </tr>
  <?php endforeach ?>
</table>
<h1>DAFTAR KERANJANG SAMPAH</h1>
<table style="width:100%" border="1">
  <tr style="background-color: yellow;">
    <td>ID</td>
    <td>name</td> 
	<td>qty</td>
	<td>price</td>
	<td>total</td>
	<td>Aksi</td>
  </tr>
  <?php foreach ($items as $items):?>
  <tr>
    <td><?php echo $items['id'];?></td>
    <td><?php echo $items['name'];?></td>
    <td>
		<form name="updateCart" method="post" action="<?php echo base_url();?>cart/update" enctype="multipart/form-data">
		<input type="text" name="qty" value="<?php echo $items['qty'];?>" maxlength="3" size="2" />
		<input type="hidden" name="rowid" value="<?php echo $items['rowid'];?>" />
		</form>
	</td>
	<td><?php $hargapro = number_format("$items[price]",0,",",".");echo "Rp.".$hargapro; ?></td>
	<td><?php 
	$totalitem = $items['qty'];
	$price = $items['price'];
	$xprice = $totalitem*$price;
	$xtotal = number_format("$xprice",0,",",".");
	echo "Rp.".$xtotal;
	?></td>
	<td>
		<form method="post" action="<?php echo base_url();?>cart/delete">
		<input type="hidden" name="id" value="<?php echo $items['rowid'];?>"/>
		<button type="submit" value="<?php echo $items['rowid'];?>">Delete</button>
		</form>
	</td>
  </tr>
  <?php endforeach ?>
</table>
<h3><b>Total Item : <?php echo $totalpro; ?></b></h3>
<h3><b>Total Harga : Rp. <?php $harga=number_format("$total",0,",",".");echo $harga; ?></b></h3>
<br>
<button type="button" onClick="update_cart();">Update Chart</button>
<a href="<?php echo base_url();?>cart/destroy"><button type="button">Clear All</button></a>

这是我的控制器更新车

// Update Product from Cart
function update(){
    $cart = $this->cart->contents();
    $data = array(
        (foreach ($cart as $c)){        
        array(
            'rowid' => $c['rowid'],
            'qty' => $this->input->post('qty'),
        ),
        };
    );
    echo "<pre>";
    print_r($data);
    //$this->cart->update($data);
    //redirect('cart/show');
}

我想问的是:1. 如何更新购物车代码点火器中的所有商品?2. 或者如何在"数组"中插入函数"foreach",就像在更新函数中一样?

我建议将rowid的值和quantity从您的form发送到更新功能。

<form action="" method="POST">
  <input type="text" name="quantity" value="" />
  <input type = "hidden" name="rowid" value=""/>
  <!-- OTHER FORM FIELDS HERE -->
  <input type="submit" name="submit" value="UPDATE" />
</form>

在您的控制器中。

public function update()
{
    $data=$this->cart->update(array(
        'rowid'=>$this->input->post('rowid'),
        'qty'=> $this->input->post('quantity');
    ));
    $this->cart->update($data);  
    //redirect here
}

更新

要一次更新所有行,您可以使用 多维数组 。它在用户指南中有很好的定义

$data = array(
               array(
                       'rowid'   => 'b99ccdf16028f015540f341130b6d8ec',
                       'qty'     => 3
                    ),
               array(
                       'rowid'   => 'xw82g9q3r495893iajdh473990rikw23',
                       'qty'     => 4
                    ),
               array(
                       'rowid'   => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
                       'qty'     => 2
                    )
            );
$this->cart->update($data);