【问题标题】:How to get the index of an array of sessions? - cakephp如何获取会话数组的索引? - cakephp
【发布时间】:2020-08-12 21:28:02
【问题描述】:

朋友,我有一个数组,我需要动态获取索引才能删除特定会话。

enter image description here

我尝试在 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


【解决方案1】:

您的“索引”似乎不是您在会话中保存的记录的一部分,而只是数组索引。您的循环需要:

foreach($this->Session->read('cart') as $index => $cart):

还有你的链接:

$this->Html->link(__('Remove'), ['action' => 'remove', $index]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2017-01-22
    • 2016-01-18
    • 2016-07-10
    • 1970-01-01
    • 2016-05-28
    • 2013-03-16
    相关资源
    最近更新 更多