How do I show the records in a table?

1

         Untitled document

 Hello, I am very new to php and my teacher has asked us to do a CRUD I have managed to show the records in the database. but, he has asked me to show them in a table and I have no idea how to do it, try to do it by entering html but given my low knowledge I could not. I hope you can help me I want to learn.     

$registros = mysqli_query( $conexion, "select *
                    from datosfrutas where id='$_POST[datoBuscar]'" )or
die( "Problemas en el select:" . mysqli_error( $conexion ) );


if ( $reg = mysqli_fetch_array( $registros ) )


{
    echo "<tr>";
    echo "El nombre es:" . $reg[ 'Nombre' ] . "<br>";
    echo "El Id es:" . $reg[ 'Id' ] . "<br>";
    echo "¿Es endemica?:" . $reg[ 'Endemica' ] . "<br>";
    echo "Pais de origen:" . $reg[ 'Pais' ] . "<br>";
    echo "Es de color:" . $reg[ 'Color' ] . "<br>";
    echo "Tiene sabor:" . $reg[ 'Sabor' ] . "<br>";
    echo "Su consistencia es:" . $reg[ 'Consistencia' ] . "<br>";
    echo "Su incidencia es:" . $reg[ 'Incidencia' ] . "<br>";

} else {
    echo "No existe una fruta con ese Id.";
}
mysqli_close( $conexion );
?>

    
asked by Digital Renegade 05.04.2018 в 00:36
source

2 answers

2

Try this:

echo "<table>";
echo "<tr><td>El nombre es:</td><td>" . $reg[ 'Nombre' ] . "</td></tr>";
echo "<tr><td>El Id es:</td><td>" . $reg[ 'Id' ] . "</td></tr>";
echo "<tr><td>¿Es endemica?:</td><td>" . $reg[ 'Endemica' ] . "</td></tr>";
echo "<tr><td>Pais de origen:</td><td>" . $reg[ 'Pais' ] . "</td></tr>";
echo "<tr><td>Es de color:</td><td>" . $reg[ 'Color' ] . "</td></tr>";
echo "<tr><td>Tiene sabor:</td><td>" . $reg[ 'Sabor' ] . "</td></tr>";
echo "<tr><td>Su consistencia es:</td><td>" . $reg[ 'Consistencia' ] . "</td></tr>";
echo "<tr><td>Su incidencia es:</td><td>" . $reg[ 'Incidencia' ] . "</td></tr></table>";
    
answered by 05.04.2018 / 00:46
source
1

You can use a loop as while for example and then using mysqli_fetch_assoc to convert the result of your query to an associative array, that is key format value.

On the other hand, leave the generic structure of the table outside the while and just put the tags tr and td inside the while to create the necessary columns and rows

<?php
$html = '<!DOCTYPE html>
        <html lang="es-Mx">
        <head>
            <meta charset="UTF-8">
            <title>Reporte de Activos</title>
        </head>
        <body>
            <h3>Reporte de Animales Disecados Activos<h3>
            <table>
            <tr> 
                <td>Nombre</td>
                <td>Familia</td>
                <td>Origen</td>
            </tr>';
            include('../librerias/conecta.php');
            $MuestraTodosInsectos = "SELECT * FROM tbanimalesdisecados WHERE status <> 0";
            $EjecutaMuestraTodosInsectos = mysqli_query($MuestraTodosInsectos);
                while($Filas = mysqli_fetch_assoc($EjecutaMuestraTodosInsectos))
                {
                    $html .= '
                  <tr>
                  <td> '.$Filas["nombre_animal"].'</td>
                  <td> '.$Filas["familia_animal"].'</td>
                  <td> '.$Filas["origen_animal"].'</td>
                  </tr>';
                }
                $html .= '</table>
        </body>
        </html>';

        echo $html;

At the end, as you can notice, I echo the variable that contains all the code.

  

I show you an example with a while because it is useful to walk n   number of records that your table has, in the same way use an entire HTML structure in the code but it is not strictly necessary; you could replace all that or remove it and just leave the code code of the table or generate the table

    
answered by 05.04.2018 в 00:57