Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Iniciar sessão:
- session_start();
- if(!isset($_SESSION['usuario'])){
- header('Location: ../index.php?msg=2');
- }
- // Importar o bd.php:
- require '../includes/db.php';
- //Conectar com o banco:
- $pdo = Banco::conectar();
- // String com a query do banco:
- $comandoSql = 'SELECT * FROM contatos ORDER BY nome';
- // Atribuição do resultado da consulta no array $resultadoConsulta:
- $resultadoConsulta = $pdo->query($comandoSql)->fetchAll(PDO::FETCH_ASSOC);
- Banco::desconectar();
- // O resultado da consulta ficará armazenado em $resultadoConsulta.
- // Caso queira visualizar o conteúdo retornado pelo bd, utilize print_r($resultadoConsulta);
- ?>
- <!DOCTYPE html>
- <html lang="pt-br">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
- <title>Listagem de Contatos</title>
- </head>
- <body>
- <h1>Contatos Cadastrados no Sistema:</h1>
- <table class="table my-4">
- <thead class="thead-dark">
- <tr>
- <th scope="col">ID</th>
- <th scope="col">Nome</th>
- <th scope="col">Telefone</th>
- <th scope="col">Endereço</th>
- </tr>
- </thead>
- <!-- Corpo (Conteúdo) da Tabela -->
- <tbody>
- <?php
- // Percorrer linha a linha do resultado e apresenta-las no corpo da tabela (tr):
- foreach ($resultadoConsulta as $linha){
- echo '<tr>';
- echo '<th scope="row">'.$linha['id'].'</th>';
- echo '<td>'.strtoupper($linha['nome']).'</td>';
- echo '<td>'.$linha['telefone'].'</td>';
- echo '<td>'.$linha['endereco'].'</td>';
- echo '</tr>';
- }
- ?>
- </tbody>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement