【发布时间】:2020-08-12 21:28:02
【问题描述】:
朋友,我有一个数组,我需要动态获取索引才能删除特定会话。
我尝试在 cart.ctp 中像这样链接删除按钮:
<html>
<body>
<!--Main layout-->
<main class="mt-1 pt-1">
<div class="container wow fadeIn">
<!-- Heading -->
<div class="row">
<!--Grid column-->
<div class="col-md-8 mb-4">
<!--Card-->
<div class="card">
<?php foreach($this->Session->read('cart') as $cart): ?>
<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">PREÇO</th>
<th scope="col">Qte</th>
<th scope="col">SUBTOTAL</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">
<?= $cart->has('product') ? $this->Html->link($cart->product->nome_product, ['controller' => 'products', 'action' => '/', $cart->product->id]) : '' ?>
</th>
<td>
<strong>R$ <?php echo number_format($cart->product->price, 2, ',', '') ?></strong>
</td>
<td>
<strong><?php echo $cart->quantity; ?> un.</strong>
</td>
<td>
<strong>
R$ <?php
$sub = ($cart->product->preco * $cart->quantidade);
echo number_format($sub, 2, ',', '');
?>
</strong>
</td>
</tr>
</tbody>
</table>
<hr width="40%">
</div>
</div>
</div>
<div class="row">
<div class="col-md-9">
</div>
<div class="col-md-1 mt-3">
</div>
<div class="col-md-1 mt-3">
<?= $this->Html->link(__('Remove'), ['action' => 'remove', $cart->index]); ?>
</div>
<div class="col-md-1">
</div>
</div>
<?php endforeach; ?>
<br>
</div>
<!--/.Card-->
</div>
<!--Grid column-->
<!--Grid column-->
<div class="col-md-4 mb-4">
<!-- Promo code -->
<form class="card p-2">
<?php echo $this->Form->end(); ?>
</div>
<!--Grid column-->
</div>
<!--Grid row-->
</div>
</main>
<!--Main layout-->
</body>
</html>
想法是将 $cart->index 变量传递给方法。
public function remove($index = mull) {
.
$cart = $this->request->session();
$cart->delete->("cart.$index");
.
但是调试($cart->index)的值为null。
你能帮我获取会话价值指数吗? (0,1,2..)
欣赏!
【问题讨论】:
-
首先,您应该直接在问题中包含任何相关详细信息。让人们点击链接来查找信息会大大减少愿意帮助您的人数。其次,看起来您可能已经从数据顶部删除了重要信息;从 0 开始计数的列没有标题?
-
您还需要在
link链接周围包含更多代码。我们可能需要查看类似foreach的内容。 -
我发布了所有代码。感谢您的任何评论
标签: php arrays session cakephp