Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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");

                'controller' => "carrito",
                'action' => 'carrito'

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

        }  else {

        $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-->

                if (isset($_SESSION["DETALLECODIGO"])) {

                    $totalcoste = 0;
                    $Total = 0;
                    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>

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

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 {

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

Just set method source -

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.


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]


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');
            $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]);

edited Dec '16

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

$id_add = any array;

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

Try figure out your problem, it's kinda analogous