如何获取会话数组的索引? - 蛋糕PHP
How to get the index of an array of sessions? - cakephp
朋友们,我有一个数组,我需要在其中动态获取索引才能删除特定会话。
enter image description here
我试过 link 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)值为空
你能帮我得到会话价值指数吗? (0,1,2..)
欣赏!
您的 "index" 似乎不是您在会话中保存的记录的一部分,而只是数组索引。你的循环需要是:
foreach($this->Session->read('cart') as $index => $cart):
还有你的link:
$this->Html->link(__('Remove'), ['action' => 'remove', $index]);
朋友们,我有一个数组,我需要在其中动态获取索引才能删除特定会话。
enter image description here
我试过 link 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)值为空
你能帮我得到会话价值指数吗? (0,1,2..)
欣赏!
您的 "index" 似乎不是您在会话中保存的记录的一部分,而只是数组索引。你的循环需要是:
foreach($this->Session->read('cart') as $index => $cart):
还有你的link:
$this->Html->link(__('Remove'), ['action' => 'remove', $index]);