Foreach taking only the last record

0

I'm developing a supplier registration screen, but my foreach is only getting the last record for a total of 3 records. What could I be doing wrong?

Below my code and attached to my screen. Many thanks

<thead><trstyle="background-color: #CEF6EC">
              <th>ID</th>
              <th>Nome Fornecedor</th>
              <th class="d-none d-sm-table-cell">Contato</th>
              <th class="d-none d-lg-table-cell">Telefone</th>
              <th width="100" class="text-center">Ações</th>
            </tr>
          </thead>
          <tbody>

            <?php
            $sql = "SELECT id,nome,con,tel FROM cad_for ";

            $sql = $pdo->query($sql);

            If($sql->rowCount()>0){
              foreach($sql->fetchAll() as $fornecedor){
              }
            }
            ?>

            <tr>
              <td><?php echo $fornecedor\['id'\]; ?></td>
              <td><?php echo $fornecedor\['nome'\]; ?></td>
              <td class="d-none d-sm-table-cell"><?php echo $fornecedor\['con'\]; ?></td>
              <td class="d-none d-lg-table-cell"><?php echo $fornecedor\['tel'\]; ?></td>

              <td>
                <a href="visualizar.php?id=<?php echo $fornecedor\['id'\]; ?>" class="btn btn-outline-primary btn-sm" data-toggle="tooltip" data-placement="left" title="Visualizar"><i class="fas fa-eye"></i></a>
                <a href="editar.php?id=<?php echo $fornecedor\['id'\]; ?>" class="btn btn-outline-warning btn-sm" data-toggle="tooltip" data-placement="left" title="Editar cadastro"><i class="fa fa-fw fa-edit"></i></a>
              </td>
            </tr>
    
asked by anonymous 30.08.2018 / 04:50

1 answer

2

Put the table row inside the foreach loop. Placing it after, will only get the last result of the loop:

<?php
$sql = "SELECT id,nome,con,tel FROM cad_for ";

$sql = $pdo->query($sql);

If($sql->rowCount()>0){
  foreach($sql->fetchAll() as $fornecedor){
?>
<tr>
   <td><?php echo $fornecedor['id']; ?></td>
   <td><?php echo $fornecedor['nome']; ?></td>
   <td class="d-none d-sm-table-cell"><?php echo $fornecedor['con']; ?></td>
   <td class="d-none d-lg-table-cell"><?php echo $fornecedor['tel']; ?></td>
   <td>
      <a href="visualizar.php?id=<?php echo $fornecedor['id']; ?>" class="btn btn-outline-primary btn-sm" data-toggle="tooltip" data-placement="left" title="Visualizar"><i class="fas fa-eye"></i></a>
      <a href="editar.php?id=<?php echo $fornecedor['id']; ?>" class="btn btn-outline-warning btn-sm" data-toggle="tooltip" data-placement="left" title="Editar cadastro"><i class="fa fa-fw fa-edit"></i></a>
   </td>
</tr>
<?php
  }
}
?>
    
30.08.2018 / 05:08