Empty list of PDO + MVC + PHP records

1

I am working on a Project Management System under the MVC pattern and the problem is when I print the records of my database in the CRUD, all the rows that correspond to my "winners" table are appreciated but empty Thanks in advance here I enclose portions of code belonging to the model, view, controller.

Model:

public function query(){
    $query = Conexion::query('SELECT * FROM triunfadores');
    $query -> execute();
    return $resultado = $query->fetchAll(PDO::FETCH_ASSOC);
}

Controller:

$triunfador = new IncluirTriunfador();
$datosT = $triunfador->query();
require_once '../vista/gestionar-triunfador-vista.php';

Vista (CRUD 'manage-triunfadores-vista.php'):

<table class="zoomIn animated table table-bordered table-responsive" style="margin:0 auto;width:74%;background-color:white;">

        <tr>
          <th class="text-center">ID</th>
          <th class="text-center">Nombre</th>
          <th class="text-center">Apellido</th>
          <th class="text-center">Cedula</th>
          <th class="text-center">Trayecto</th>
           <th class="text-center">Sección</th>
          <th class="text-center">Condición</th>
          <th class="text-center">Acciones</th>
        </tr> 

        <?php

          foreach($datosT as $persona){

        ?>

        <tr>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["id"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["nombre_triun"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["apellido_triun"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["cedula_triun"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["trayecto_triun"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["seccion_triun"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["condicion_triun"]; ?></td>
          <td>
            <a href="../controlador/anadir-triunfador-controlador.php"><input style="width:88px;" type="text" class="btn btn-success" value="Añadir"></a>
            <a href="../controlador/actualizar-triunfador-controlador.php"><input style="width:88px;" type="text" class="btn btn-primary" value="Actualizar"></a>
            <a href="../controlador/buscar-triunfador-controlador.php"><input style="width:88px;" type="text" class="btn btn-info" value="Buscar"></a>
            <a href="../controlador/eliminar-triunfador-controlador.php?<?php echo "id=1"; ?>"><input style="width:88px;" type="text" class="btn btn-danger" value="Eliminar"></a>
          </td>

        </tr>

        <?php

         }

        ?>


  </table>
    
asked by Alfredo Fernández 08.03.2018 в 03:37
source

2 answers

1

The error

The table appears empty because in foreach you have put this:

      foreach($datosT as $persona){

But then, when looking for each data by its columns, you do this for example:

echo $datosT["id"]

The solution

To find the data by its columns you must use $persona , not $datosT .

Optimization and clarity proposal

Seen the seen, the code can be improved, if you want it.

  • Since this is repeated again and again <td class="text-center" style="font-size:13px;"> , you can put it in a variable, here I have called it $tdOn . To maintain a standard, I have also created a variable to close the cells, to which I have called $tdOff . In those repetitive cases we will use that variable. By the way, it is not a good practice to put styles in the elements directly . It is recommended to use CSS for styles. In this case, you can indicate font-size:13px; for td of class text-center in CSS. This will allow, in case you want to change that data one day, do it in one place (in the CSS), and not have to go to find all the tables and td wherever they are to change them.
  • Also, in my opinion, the code gains a lot of clarity when you avoid mixing pieces of PHP with HTML. To do that we can concatenate everything in a single variable, work everything in PHP and do echo of that variable at the end of the whole.

    <?php
    $tdOn='<td class="text-center" style="font-size:13px;">';
    $tdOff='</td>'; 
    $tablaHTML='<table class="zoomIn animated table table-bordered table-responsive" style="margin:0 auto;width:74%;background-color:white;">';
    
    $tablaHTML.=
            '<tr>
              <th class="text-center">ID</th>
              <th class="text-center">Nombre</th>
              <th class="text-center">Apellido</th>
              <th class="text-center">Cedula</th>
              <th class="text-center">Trayecto</th>
               <th class="text-center">Sección</th>
              <th class="text-center">Condición</th>
              <th class="text-center">Acciones</th>
            </tr>';         
    
              foreach($datosT as $persona){
                $tablaHTML.='<tr>';
                    $tablaHTML.=$tdOn.$persona["id"].$tdOff;
                    $tablaHTML.=$tdOn.$persona["nombre_triun"].$tdOff;
                    $tablaHTML.=$tdOn.$persona["apellido_triun"].$tdOff;
                    $tablaHTML.=$tdOn.$persona["cedula_triun"].$tdOff;
                    $tablaHTML.=$tdOn.$persona["trayecto_triun"].$tdOff;
                    $tablaHTML.=$tdOn.$persona["seccion_triun"].$tdOff;
                    $tablaHTML.=$tdOn.$persona["condicion_triun"].$tdOff;
                    $tablaHTML.='<td>
                    <a href="../controlador/anadir-triunfador-controlador.php"><input style="width:88px;" type="text" class="btn btn-success" value="Añadir"></a>
                    <a href="../controlador/actualizar-triunfador-controlador.php"><input style="width:88px;" type="text" class="btn btn-primary" value="Actualizar"></a>
                    <a href="../controlador/buscar-triunfador-controlador.php"><input style="width:88px;" type="text" class="btn btn-info" value="Buscar"></a>
                    <a href="../controlador/eliminar-triunfador-controlador.php?<?php echo "id=1"; ?>"><input style="width:88px;" type="text" class="btn btn-danger" value="Eliminar"></a>
                  </td>';
                $tablaHTML.='</tr>';
             }
    $tablaHTML.='</table>';
    echo $tablaHTML;
    ?>
    
answered by 08.03.2018 / 11:06
source
0

because you do not do a var_dump where I tell you to see what data you bring to $datosT if that is the case.

<table class="zoomIn animated table table-bordered table-responsive" style="margin:0 auto;width:74%;background-color:white;">

        <tr>
          <th class="text-center">ID</th>
          <th class="text-center">Nombre</th>
          <th class="text-center">Apellido</th>
          <th class="text-center">Cedula</th>
          <th class="text-center">Trayecto</th>
           <th class="text-center">Sección</th>
          <th class="text-center">Condición</th>
          <th class="text-center">Acciones</th>
        </tr> 

        <?php
var_dump($datosT);
          foreach($datosT as $persona){

        ?>

        <tr>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["id"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["nombre_triun"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["apellido_triun"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["cedula_triun"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["trayecto_triun"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["seccion_triun"]; ?></td>
          <td class="text-center" style="font-size:13px;"><? echo $datosT["condicion_triun"]; ?></td>
          <td>
            <a href="../controlador/anadir-triunfador-controlador.php"><input style="width:88px;" type="text" class="btn btn-success" value="Añadir"></a>
            <a href="../controlador/actualizar-triunfador-controlador.php"><input style="width:88px;" type="text" class="btn btn-primary" value="Actualizar"></a>
            <a href="../controlador/buscar-triunfador-controlador.php"><input style="width:88px;" type="text" class="btn btn-info" value="Buscar"></a>
            <a href="../controlador/eliminar-triunfador-controlador.php?<?php echo "id=1"; ?>"><input style="width:88px;" type="text" class="btn btn-danger" value="Eliminar"></a>
          </td>

        </tr>

        <?php

         }

        ?>


  </table>

so you can focus more or less where the error is and we can help you better.

    
answered by 08.03.2018 в 10:39