Advertisement
estevaorada

Apoio - Listagem de Contatos

Feb 10th, 2022
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.06 KB | None | 0 0
  1. <?php
  2. // Iniciar sessão:
  3. session_start();
  4. if(!isset($_SESSION['usuario'])){
  5.     header('Location: ../index.php?msg=2');
  6. }
  7.  
  8. // Importar o bd.php:
  9. require '../includes/db.php';
  10.  
  11. //Conectar com o banco:
  12. $pdo = Banco::conectar();
  13. // String com a query do banco:
  14. $comandoSql = 'SELECT * FROM contatos ORDER BY nome';
  15. // Atribuição do resultado da consulta no array $resultadoConsulta:
  16. $resultadoConsulta = $pdo->query($comandoSql)->fetchAll(PDO::FETCH_ASSOC);
  17.  
  18. Banco::desconectar();
  19.  
  20. // O resultado da consulta ficará armazenado em $resultadoConsulta.
  21. // Caso queira visualizar o conteúdo retornado pelo bd, utilize print_r($resultadoConsulta);
  22. ?>
  23. <!DOCTYPE html>
  24. <html lang="pt-br">
  25. <head>
  26.     <meta charset="UTF-8">
  27.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  28.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  29.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  30.     <title>Listagem de Contatos</title>
  31. </head>
  32. <body>
  33.     <h1>Contatos Cadastrados no Sistema:</h1>
  34.     <table class="table my-4">
  35.         <thead class="thead-dark">
  36.             <tr>
  37.                 <th scope="col">ID</th>
  38.                 <th scope="col">Nome</th>
  39.                 <th scope="col">Telefone</th>
  40.                 <th scope="col">Endereço</th>
  41.             </tr>
  42.         </thead>
  43.         <!-- Corpo (Conteúdo) da Tabela -->
  44.         <tbody>
  45.             <?php
  46.             // Percorrer linha a linha do resultado e apresenta-las no corpo da tabela (tr):
  47.             foreach ($resultadoConsulta as $linha){
  48.                 echo '<tr>';
  49.                 echo '<th scope="row">'.$linha['id'].'</th>';
  50.                 echo '<td>'.strtoupper($linha['nome']).'</td>';
  51.                 echo '<td>'.$linha['telefone'].'</td>';
  52.                 echo '<td>'.$linha['endereco'].'</td>';
  53.                 echo '</tr>';
  54.             }
  55.             ?>
  56.         </tbody>
  57.     </table>
  58. </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement