Extract a column of MSQL names and save it in a PHP array

-1

What I want is to extract the names from the database and save them in an array and then display them according to the order as I saved them in the array I want to save it in the $ student []

array
    <?php
                               $nombre = array();
                                  include_once '../conexion.php';
                                  $mysqli = new mysqli($hostbd,$usuariobd,$clave,$basededatos);
                                    if ($mysqli -> connect_errno) {
                                      die("Fallo Conexion a MySQL");
                                    }
                                      else{
                                        $sql_query="SELECT Status,Alumno FROM computadoras WHERE Fila='1'";
                                        $resultado=$mysqli->query($sql_query);
                                          while($row=$resultado->fetch_assoc())
                                            {
                                             $status[] = $row["Status"];
                                             $alumno[]= $row["Alumno"];
                                            }
                                  }
?>

This is where I call it to print

<div class="fila">

      <?php
      for ($i=0; $i <10 ; $i++) { 
        if ($status[$i]!=0) { 
      ?>
             <div id="info">
               <img id="foto" src="img/computer(1).png" alt="computer"><span>Disponible</span>
             </div>
        <?php
         }
         else{ ?>
           <div id="info">
              <img id="foto" src="img/computer.png" alt="computer"><span>Matricula: <?php echo $alumno[$i]; ?></span>
           </div>
            <?php
         }

      }

      ?>


</div>

The status if it works for me, because it's only 1 or 0 if it's 1 prints something else, but when it's in use I want to know the name of the person who is using it

    
asked by DanielDaniel 07.09.2018 в 20:13
source

1 answer

0

I think we can simplify this into something more readable, I'll leave you a commented example. You have many error control errors and validations, but I think that for this case this will suffice, take care of the rest.

<?php
//Aqui está raro, por que estas incluyendo la conexión y la estas declarando con variables que ni existen
//Lo dejaré así pero debes corregirlo (A menos que uses dos conexiones)
include_once '../conexion.php';
$mysqli = new mysqli($hostbd,$usuariobd,$clave,$basededatos);
if ($mysqli -> connect_errno) {
  die("Fallo Conexion a MySQL");
}

else{
  //Creamos una variable que contendrá nuestra información
  $data = array();
  $sql_query = "SELECT Status,Alumno FROM computadoras WHERE Fila='1'";
  $resultado = $mysqli->query( $sql_query );

  //Ciclamos el resultado
  while( $row = $resultado->fetch_assoc() ){
    //Agregamos al indice en curso y a los índices status y nombre 
    //Sus respectivos valores
    $data[]['status'] = (int) $row['Status'];
    $data[]['nombre'] = $row['Alumno'];
  }
}

//Defines una variable que tendrá todo tu HTML
$html = "";

//Empiezas a concatenar las piezas
$html .= '
<div class="fila">';

//Ciclas la variable data y sacas la información armando tu HTML
foreach( $data as $d ){
  if( $d['status'] === 0 ){
    $html .= '
    <div id="info">
      <img id="foto" src="img/computer.png" alt="computer"><span>Matricula: '. $d['nombre'] .'</span>
    </div>';
  }

  else{
    '<div id="info">
      <img id="foto" src="img/computer(1).png" alt="computer"><span>Disponible</span>
    </div>';
  }
}

$html .='</div>';

//Al final imprimes tu variable del HTML
echo $html;
?>
    
answered by 07.09.2018 в 21:02