Help with array of objects in $ _SESSION PHP and MySQL

0

I would like to know how I could do to load data from a BB DD and with that data loaded create an object for each row of the database that would be stored in an array of objects with $_SESSION

for example, using this code:

CLASSIC CHASSIS CLASSIFIED     

class Chasis_Acorazado {
    protected $Numero_serie;
    protected $Instante_Activacion;
    protected $Estado_Operativo;
    protected $Fuente_Energia;
    protected $Material_Coraza;
    ///////////////////////////////////////////
    /*<<<<<<<<<<<<<< CONSTRUCTOR >>>>>>>>>>>>*/
    ///////////////////////////////////////////
    public function __construct($Numero_serie,$Instante_Activacion,$Estado_Operativo,
            $Fuente_energia,$Material_Coraza)
    {
        $this->Estado_Operativo=$Estado_Operativo;
        $Estado_Operativo='ON';
        $this->Instante_Activacion=$Instante_Activacion;
        $this->Numero_serie=$Numero_serie;
        $this->Fuente_Energia=$Fuente_energia;
        $this->Material_Coraza=$Material_Coraza;
    }

    ///////////////////////////////////////
    /*<<<<<<<<<<<<<< METODOS >>>>>>>>>>>>*/
    ///////////////////////////////////////
    public function Conmutar_Estado()
    {
    if($Estado_Operativo=='ON')
        {
        $Estado_Operativo='OFF';
        return ($Estado_Operativo);
        }
    else
        {
        $Estado_Operativo='ON';
        return($Estado_Operativo);
        }
    }
    public function Scout_Finch ()
    {
        $Estado_Operativo='OFF';
        return($Estado_Operativo);
    }

       //////////////////////////////////////////////////////////////
       /*<<<<<<<<<<<<<< Modificadores y Visualizadores >>>>>>>>>>>>*/
       //////////////////////////////////////////////////////////////
       public function getNumero_serie() {
           return $this->Numero_serie;
       }

       public function getInstante_Activacion() {
           return $this->Instante_Activacion;
       }

       public function getEstado_Operativo() {
           return $this->Estado_Operativo;
       }

       public function getFuente_Energia() {
           return $this->Fuente_Energia;
       }

       public function getMaterial_Coraza() {
           return $this->Material_Coraza;
       }

       public function setNumero_serie($Numero_serie) {
           $this->Numero_serie = $Numero_serie;
       }

       public function setInstante_Activacion($Instante_Activacion) {
           $this->Instante_Activacion = $Instante_Activacion;
       }

       public function setEstado_Operativo($Estado_Operativo) {
           $this->Estado_Operativo = $Estado_Operativo;
       }

       public function setFuente_Energia($Fuente_Energia) {
           $this->Fuente_Energia = $Fuente_Energia;
       }

       public function setMaterial_Coraza($Material_Coraza) {
           $this->Material_Coraza = $Material_Coraza;
       }


}

ARCHIVE THAT CREATES OBJECTS ARMORED CHASSIS AND GUARDS THEM IN PERSISTENCE ON THE BB DD

 <?php
require_once 'Chasis_Acorazado.php';
session_start();



if(isset($_POST['NS'])&& isset($_POST['IA'])&& isset($_POST["EO"])&& isset($_POST["FE"])&& isset($_POST["MC"])){
$numeroSerie = $_POST['NS'];
$instanteActivacion = $_POST['IA'];  
$estadoOperativo = $_POST['EO'];
$fuenteEnergia = $_POST['FE'];
$materialCoraza = $_POST['MC'];
$chasisAcorazado=array();

// Nos conectamos a la base de datos:  
     $conexion = mysqli_connect ('localhost', 'control', 'control', 'control');


     $CA= new Chasis_Acorazado($numeroSerie, $instanteActivacion, $estadoOperativo, $fuenteEnergia, $materialCoraza);



     $sql ="INSERT INTO chasis_acorazado(numero_serie, instante_activacion, estado_operativo, fuente_energia, material_coraza) VALUES ('$numeroSerie', '$instanteActivacion', '$estadoOperativo', '$fuenteEnergia', '$materialCoraza')" ;
    $_SESSION['chasis'][$numeroSerie]=$CA;


     if (!$resultado = $conexion->query($sql)) 
      {
       // ¡Oh, no! La consulta falló. 
       echo "ERROR: La consulta fracasó";  
       print"<br><br><a href=\"javascript:history.go(-1);\">Volver</a>";
      }
     else
      {
       echo'Usted a insertado un chasis acorazado con numero de serie: '. $CA->getNumero_serie().' cuyo instante de activacion es: '.$CA->getInstante_Activacion().' con una fuente de energia basada en: '. $CA->getFuente_Energia().' y hecho de: '. $CA->getMaterial_Coraza();  ; 
      print"<br><br><a href=\"javascript:history.go(-1);\">Volver</a>";
      }

}
else
{
    echo'Debe rellenar los campos';
    print"<br><br><a href=\"javascript:history.go(-1);\">Volver</a>";
}
?>

DATABASE USED:

 CREATE DATABASE control;
CREATE TABLE chasis_acorazado
(
numero_serie VARCHAR (20) NOT NULL PRIMARY KEY,
instante_activacion INT NOT NULL,
estado_operativo VARCHAR (4) NOT NULL,
fuente_energia VARCHAR (10) NOT NULL,
material_coraza VARCHAR (10) NOT NULL
);

Thank you very much in advance.

    
asked by Alex Vic 20.04.2018 в 18:43
source

1 answer

0

If what you need is for the list of records in the chasis_acorazado table to be maintained in $_SESSION while the user visits and uses your application, it would be a SELECT when the user enters the application. You do not have to do the SELECT again if the data is already stored in $_SESSION . At best, make a quick query to find out if there were changes (modifications or insertions), but not to alter the entire array. In fact, when you already do your Insert in your code, you are adding a new element to the array, which is already perfect.

(As I do not know the operation of your class that handles queries, I explain everything to you in pseudocode).

  • At the beginning of your main script, ask if the user has just started a session. If so, you execute a query to the BD that returns all numero_serie of chasis_acorazado .
  • You scroll the result, instantiating for each row an object type Chasis_Acorazado .
  • You store in the array $_SESSION each of the objects, within the same cycle, following the same way you do it now with the INSERT .
  • answered by 20.04.2018 / 20:01
    source