how to keep two rows of records in $ _SESSION?

0

I'm looking for a way to keep in $ _SESSION two or more rows of records that are thrown by a mysql query, and then use those results in other queries. I usually pulled a single record row with:

while($row=mysql_fetch_array($consultar))
            {
            $_SESSION["cedula"]=$row["cedula"];
            $_SESSION["nombre"]=$row["nombre"];
            $_SESSION["apellido"]=$row["apellido"];
            }

but that would only keep one row in session now I need you to keep several rows in session of this query:

$consultar=mysql_query("SELECT * from asignacion where cedula='$_SESSION[cedula]'");
$num=mysql_num_rows($consultar);
if($num==0)
    {
  echo "<script>alert('¡NO HAY EQUIPOS ASIGNADOS A ESTE EMPLEADO!')</script>";
  echo "<meta http-equiv='refresh' content='0; url=../vista/devolucion.php'>";
}else{
  while($row=mysql_fetch_array($consultar))
          {

    $_SESSION["id_case"]=$row["id_case"];
    $_SESSION["periferico1"]=$row["periferico1"];
    $_SESSION["periferico2"]=$row["periferico2"];
    $_SESSION["periferico3"]=$row["periferico3"];
    $_SESSION["id_laptop"]=$row["id_laptop"];
    }

echo "<script>alert('¡ENCONTRADO!')</script>";
}

and be able to use it to generate a dynamic table with those records found

<?php

    $sqlQuery = mysql_query("SELECT * from equipo where id_case='$_SESSION[id_case]'");
    $table = $sqlQuery or die(mysql_error());
    if ($table) {
        ?>

but everything is dynamic, you do not know how many records the user will make, I hope you can help me and thanks in advance

    
asked by Bryant Medina 05.11.2016 в 14:48
source

2 answers

1

It is understood that you can only record the data of a single user in session so your first query works but you can improve it, you can research mysql_fetch_row

In the second part you are overwriting the results, so you should save it as an arrangement, change to:

while($row = mysql_fetch_array($consultar, MYSQL_ASSOC)) {
    $_SESSION['asignaciones'][] = $row;
}

To recover it in the dynamic tables you use:

<?php
    foreach($_SESSION['asignaciones'] as $asignacion) {
        $query = mysql_query("SELECT * FROM equipo WHERE id_case='".$asignacion['id_case']."'");
       .. genera tabla equipos por id_case ..
    }
?>

Greetings

    
answered by 05.11.2016 в 16:39
0

You can store a multidimensional array on an element of the session:

$_SESSION["asignaciones"] = array();

while($row=mysql_fetch_array($consultar)) {
    $asignacion = array();
    $asignacion["id_case"] = $row["id_case"];
    $asignacion["periferico1"] = $row["periferico1"];
    $asignacion["periferico2"] = $row["periferico2"];
    $asignacion["periferico3"] = $row["periferico3"];
    $asignacion["id_laptop"] = $row["id_laptop"];
    $_SESSION["asignaciones"][] = $asignacion;
}
    
answered by 05.11.2016 в 16:29