index


SUBMITTED BY: Lucas312

DATE: Dec. 13, 2016, 12:34 a.m.

FORMAT: PHP

SIZE: 2.5 kB

HITS: 102974

  1. <?php
  2. require_once 'init.php';
  3. // abre a conexão
  4. $PDO = db_connect();
  5. // SQL para contar o total de registros
  6. // A biblioteca PDO possui o método rowCount(), mas ele pode ser impreciso.
  7. // É recomendável usar a função COUNT da SQL
  8. // Veja o Exemplo 2 deste link: http://php.net/manual/pt_BR/pdostatement.rowcount.php
  9. $sql_count = "SELECT COUNT(*) AS total FROM users ORDER BY name ASC";
  10. // SQL para selecionar os registros
  11. $sql = "SELECT id, name, email, gender, birthdate FROM users ORDER BY name ASC";
  12. // conta o toal de registros
  13. $stmt_count = $PDO->prepare($sql_count);
  14. $stmt_count->execute();
  15. $total = $stmt_count->fetchColumn();
  16. // seleciona os registros
  17. $stmt = $PDO->prepare($sql);
  18. $stmt->execute();
  19. ?>
  20. <!doctype html>
  21. <html>
  22. <head>
  23. <meta charset="utf-8">
  24. <title>LHSS DIGITAL - Cadastro</title>
  25. </head>
  26. <body>
  27. <h1>LHSS DIGITAL - Cadastro</h1>
  28. <p><a href="form-add.php">Adicionar Usuário</a></p>
  29. <h2>Lista de Usuários</h2>
  30. <p>Total de usuários: <?php echo $total ?></p>
  31. <?php if ($total > 0): ?>
  32. <table width="50%" border="1">
  33. <thead>
  34. <tr>
  35. <th>Nome</th>
  36. <th>Email</th>
  37. <th>Gênero</th>
  38. <th>Data de Nascimento</th>
  39. <th>Idade</th>
  40. <th>Ações</th>
  41. </tr>
  42. </thead>
  43. <tbody>
  44. <?php while ($user = $stmt->fetch(PDO::FETCH_ASSOC)): ?>
  45. <tr>
  46. <td><?php echo $user['name'] ?></td>
  47. <td><?php echo $user['email'] ?></td>
  48. <td><?php echo ($user['gender'] == 'm') ? 'Masculino' : 'Feminino' ?></td>
  49. <td><?php echo dateConvert($user['birthdate']) ?></td>
  50. <td><?php echo calculateAge($user['birthdate']) ?> anos</td>
  51. <td>
  52. <a href="form-edit.php?id=<?php echo $user['id'] ?>">Editar</a>
  53. <a href="delete.php?id=<?php echo $user['id'] ?>" onclick="return confirm('Tem certeza de que deseja remover?');">Remover</a>
  54. </td>
  55. </tr>
  56. <?php endwhile; ?>
  57. </tbody>
  58. </table>
  59. <?php else: ?>
  60. <p>Nenhum usuário registrado</p>
  61. <?php endif; ?>
  62. </body>
  63. </html>

comments powered by Disqus