We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Array of sessions

Hello, I am trying to create a session array every time I enter the controller, a new row is added to my array and it is displayed in the view. Something like a web shopping cart

my controller


    public function PedidoAction($SEART_CODIGO) {
        $seart_articulo = SeartArticulo::findFirstBySEART_CODIGO($SEART_CODIGO);
        if (!$seart_articulo) {
            $this->flash->error("Articulo No encontrado");

            $this->dispatcher->forward(array(
                'controller' => "carrito",
                'action' => 'carrito'
            ));

            return;
        }
        if ($this->session->has("id")) {
        $_SESSION['id']++;

        }  else {
            $this->session->set('id',0);
        }
        $id=$_SESSION['id'];

        $this->session->set('DETALLECODIGO[]', $seart_articulo->SEART_CODIGO);
        $this->session->set('DETALLENOMBRE[]', $seart_articulo->SEART_DETALLE);
        $this->session->set('DETALLEPRECIO[]', $seart_articulo->SEART_PRECIOX);
        $this->session->set('DETALLEIVA[]', $seart_articulo->SEART_DETALLE);
        $this->session->set('DETALLECANTID[]', '1');
        $this->session->set('DETALLETOTAL[]', $seart_articulo->SEART_PRECIOX);

    }

And what I want to show in my view

<table class="table" table-bordered>
            <!--table lista/table-row-group-striped-->
            <thead>
                <tr>
                    <th>

                    </th>
                    <th>Cant</th>
                    <th>Descripción</th>             
                    <th>Iva</th>
                    <th>Precio</th>
                    <th>Total</th>
                    <th>Quitar</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if (isset($_SESSION["DETALLECODIGO"])) {

                    $totalcoste = 0;
                    $Total = 0;
                    $n=count($_SESSION['DETALLECODIGO']);
                    echo $n;
                    echo $_SESSION['id'];
                    for ($i=0; $i<=$_SESSION['id']; $i++) {
                        echo "<td>". $i."</td>
                        <td> ". $this->session->get('DETALLECODIGO'.[$i])."</td>
                    <td>". $this->session->get('DETALLENOMBRE'.[$i])."</td>
                    <td>". $this->session->get('DETALLECANTID'.[$i])." </td>
                    <td>". $this->session->get('DETALLEIVA'.[$i])." </td>
                    <td>". $this->session->get('DETALLEPRECIO'.[$i])." </td>
                    <th>Quitar</th>";
                    }
                }
                ?>

                <tr>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="10"><strong>Cantidad:</strong> <span id="span_cantidad">0</span> Items.</td>
                </tr>
            </tfoot>
        </table>

But nothing is displayed using $ id and if I use count () only one record is displayed, the array is not incremented. What is the problem?

edited Dec '16

It might be this:

 $_SESSION['id']++; // you are incrementing a variable in session, but not saving it anywhere, try 

$new_id_session =  $_SESSION['id']++;

}  else {
    $this->session->set('id',0);
}

$id = $_SESSION['id']; // =$new_id_session

Just set method source - https://github.com/phalcon/cphalcon/blob/master/phalcon/session/adapter.zep

ALSO USE $this->session everywhere, stop using $_SESSION if there is session adapter.

You need to save array, syntax like

$this->session->set('DETALLECODIGO[]', $seart_articulo->SEART_CODIGO);

wont magically create array as DETALLECODIGO key in session and add $seart_articulo->SEART_CODIGO value.



81.1k

What is the correct way to create the session matrix?

Car [code] [0] Car [code] [1] Car [code] [3] Car [name] [0] Car [name] [1] Car [name] [3]



81.1k

I am already showing the data but every time I execute this statement the array is modified I do not add a new row, it is edited the one already created.


     if ($this->session->has("id")) {

            $a = $this->session->get('id');
            $a++;
            $this->session->set('id', $a);
        } else {
            $this->session->set('id', 0);
        }
        $id = $this->session->get('id');

        $this->session->set('DETALLECODIGO',[$id=> $seart_articulo->SEART_CODIGO]);
        $this->session->set('DETALLENOMBRE',[$id=> $seart_articulo->SEART_DETALLE]);
        $this->session->set('DETALLEPRECIO',[$id=> $seart_articulo->SEART_PRECIOX]);
        $this->session->set('DETALLEIVA', [$id=>$seart_articulo->SEART_DETALLE]);
        $this->session->set('DETALLECANTID', [$id=>'1']);
        $this->session->set('DETALLETOTAL', [$id=>$seart_articulo->SEART_PRECIOX]);


6.0k
Accepted
answer
edited Dec '16

it's because your variable $id isn't an array. According to this working example:

$id_add = any array;
  if($session->has("koszyk"))
                        {

                            $koszyk_array = $session->get("koszyk");
                            $koszyk_array[] = $id_add;
                            $session->set("koszyk", $koszyk_array);
                        }
                        else
                        {
                            $koszyk_array[] = $id_add;
                            $session->set("koszyk",$koszyk_array);
                        }

Try figure out your problem, it's kinda analogous