Skip to content
Advertisement

How to get the index of an array of sessions? – cakephp

Friends, I have an array where I need to get the index dynamically to be able to delete the specific session.

enter image description here

I tried to link the delete button like this in 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>

The idea is to pass the $cart->index variable to the method.

public function remove($index = mull) {
.
$cart = $this->request->session();
$cart->delete->("cart.$index");
.

But debugging ($cart->index) the value is null.

Can you help me get the session value index? (0,1,2..)

Appreciate!

Advertisement

Answer

Your “index” appears to not be a part of the records you have saved in your session, but rather just the array index. Your loop needs to be:

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

And your link:

$this->Html->link(__('Remove'), ['action' => 'remove', $index]);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement