Error when listing in php with database

0

This code shows me a single row of the database, when in my database I have 11 rows, to which this should be written. Thanks in advance         

    while($row = mysqli_fetch_array($result)){
    // Array temporal para crear una sola categoría
    $tmp = array();
    ?>


    <tr>       
                <td><?php $tmp["id"] = $row["id"];  echo $tmp["id"]; ?></td>
                <td><?php $tmp["estado"] = $row["estado"]; echo $tmp["estado"]; ?></td>
                <td><?php $tmp["lumen"] = $row["lumen"]; echo $tmp["lumen"]; ?></td>
                <td><?php $tmp["fecha"] = $row["fecha_hora"]; echo $tmp["fecha"]; ?></td>
                <td>
                    <a href="<?php echo base_url('blog/edit/'); ?>" class="btn btn-info">Edit</a>
                    <a href="<?php echo base_url('blog/delete/'); ?>" class="btn btn-danger onclick="return confirm('Do you want to delete this record?');">Delete</a>
                </td>


    </tr>
    <?php 

my complete code is this

<?php
//  include_once './conexion.php';
      include 'conexion.php';
    $respuesta = array();
    $respuesta["datos"] = array();  

// Conectarse al servidor y seleccionar base de datos.
$con = mysqli_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysqli_select_db($con,"$db_name")or die("cannot select DB");
$sql="SELECT * FROM lumen";
$result=mysqli_query($con,$sql);


?>
<head>
    <title>Listado</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <style>
      .container{margin-top:100px}
    </style>
  </head>
<body>
<table class="table table-bordered table-responsive" style="width:100%;">
            <thead>
                <tr>
                    <td>ID</td>
                    <td>Estado</td>
                    <td>Valor</td>

                    <td>Fecha y Hora</td>

                </tr>
            </thead>
            <tbody>
            <br>

        <?php    

        while($row = mysqli_fetch_array($result)){
        // Array temporal para crear una sola categoría
        $tmp = array();
        ?>


        <tr>       
                    <td><?php $tmp["id"] = $row["id"];  echo $tmp["id"]; ?></td>
                    <td><?php $tmp["estado"] = $row["estado"]; echo $tmp["estado"]; ?></td>
                    <td><?php $tmp["lumen"] = $row["lumen"]; echo $tmp["lumen"]; ?></td>
                    <td><?php $tmp["fecha"] = $row["fecha_hora"]; echo $tmp["fecha"]; ?></td>
                    <td>
                        <a href="<?php echo base_url('blog/edit/'); ?>" class="btn btn-info">Edit</a>
                        <a href="<?php echo base_url('blog/delete/'); ?>" class="btn btn-danger onclick="return confirm('Do you want to delete this record?');">Delete</a>
                    </td>


        </tr>
        <?php 

          array_push($respuesta["datos"], $tmp);
        }
        ?>


    </tbody>
    </table>

    <body>
    
asked by Ayrton Axel 22.07.2018 в 00:38
source

1 answer

0

Firstly, the Manual does not say that mysqli_fetch_array creates an associative array by default, therefore, it is advisable to indicate that parameter: while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

Then, I do not see what sense your $tmp array has, it has no other function than overloading the code with something unnecessary.

Try this mode. I have also tried to write a clearer code, without so much mixture of PHP / HTML that makes the code illegible.

<?php 
    //El código de más arriba puedes dejarlo como lo tienes
    $btnEdit='<a href="'.base_url('blog/edit/').'" class="btn btn-info">Edit</a>';
    $btnDelete='<a href="'.base_url('blog/delete/').'" class="btn btn-danger onclick="return confirm('Do you want to delete this record?');">Delete</a>';

    $strHTML="";   //declaramos aquí la variable de concatenación

    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){

        $id=$row["id"];
        $estado=$row["estado"];
        $lumen=$row["lumen"];
        $fecha=$row["fecha_hora"];
        $strHTML.="<tr>";       
                $strHTML.="<td>$id</td>";
                $strHTML.="<td>$estado</td>";
                $strHTML.="<td>$lumen</td>";
                $strHTML.="<td>$fecha</td>";
                $strHTML.="<td>$btnEdit</td>";
                $strHTML.="<td>$btnDelete</td>";
        $strHTML.="</tr>";       
    } //Cerramos el while
echo $strHTML;

The code that follows can be left as you have it or continue to concatenate your $strHTML to give uniformity to the style in which programs.

    
answered by 22.07.2018 / 01:30
source