【发布时间】:2020-08-16 05:51:26
【问题描述】:
朋友们,我正在尝试根据on this article 更新我的购物车。
但是,我无法在会话中单独更新“数量”值。该文章适用于在数据库中注册的产品。我使用会话。我相信 cart.ctp 中有一些细节在这个过渡中是不正确的!如果有人能看到发生了什么,我将不胜感激!
我的cart.ctp代码如下:
<html>
<body>
<main class="mt-1 pt-1">
<div class="container wow fadeIn">
<div class="row">
<div class="col-md-8 mb-4">
<div class="card">
<?php foreach($this->Session->read('carrinho') as $index=>$carrinho): ?>
<div class="container">
<div class="row">
<div class="col-md-10">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">product</th>
<th scope="col">price</th>
<th scope="col">Qte</th>
<th scope="col">subtotal</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">
<?= $carrinho->has('produto') ? $this->Html->link($carrinho->produto->nome_produto, ['controller' => 'Produtos', 'action' => '/', $carrinho->produto->id]) : '' ?>
</th>
<td>
<strong>R$ <?php echo number_format($carrinho->produto->preco, 2, ',', '') ?></strong>
</td>
<td>
<?php echo $this->Form->create('Pedidos',array('id'=>'add-form',
'url'=>array('controller'=>'pedidos','action'=>'update',$carrinho->produto->id)));?>
<?php
echo $this->Form->control('quantidade',
array('type'=>'number', 'label'=>false,'min'=> 1,'size' => 2,
'maxlenght' => 2, 'class'=>'form-control','required' => 'true', 'value'=>$carrinho->quantidade));?> un.
<?php echo $this->Form->submit('update',array('class'=>'btn-md text-white waves-effect m-1', 'style' => 'background-color:#1ab394'));?>
<?php echo $this->Form->end();?>
</td>
<td>
<strong>
R$ <?php
$sub = ($carrinho->produto->preco * $carrinho->quantidade);
echo number_format($sub, 2, ',', '');
?>
</strong>
<?php $total = array(number_format($sub, 2, ',', ''));?>
</td>
</tr>
</tbody>
</table>
<hr width="40%">
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
</div>
<div class="col-md-2 mt-3">
<?= $this->Html->link(__('Update'), ['action' => 'update']); ?>
</div>
<div class="col-md-2 mt-3">
<?= $this->Html->link(__('Delete'), ['action' => 'delete', $index]); ?>
</div>
</div>
<?php endforeach; ?>
<br>
</div>
</div>
<div class="col-md-4 mb-4">
<form class="card p-2">
<div class="input-group">
<?= $this->Html->link(__('Checkout'), ['action' => 'checkout']); ?>
</div>
<?php echo $this->Form->end(); ?>
</div>
</div>
</div>
</main>
</body>
</html>
cart.php
public function update($productId = null) {
$session = $this->request->session();
$carrinho = $session->read('carrinho');
$quantidade = $this->request->data('quantidade');
if ($quantidade > 0) {
$carrinho[$productId] = $quantidade;
$session->write('carrinho', $carrinho);
return $this->redirect(['action' => 'index']);
}
}
【问题讨论】:
-
也许告诉我们你遇到了什么错误,并指出它发生在哪一行?
-
金额值来自输入时为空。 echo $this->Form->input('数量',..
-
这里有很多错误,很抱歉。最大的问题似乎是对 HTML 表单的一般工作方式以及输入名称和发布的数据之间的关系存在严重误解。在提出解决方案之前,我们需要知道,您是希望能够一次更新一堆数量,还是一次只更新一个?
-
一次一个。我对代码进行了更新。我在 foreach 中遇到问题:为 foreach () 提供的参数无效。现在我可以带来新数量的值,但它在 foreach 中给出了这个错误。