Advertisement
MizunoBrasil

paginação legal

Mar 9th, 2023 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.20 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <title>Lista de Adultos</title>
  5.   <!-- Bootstrap CSS -->
  6.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9.  
  10. <div class="container">
  11.   <h2>Lista de Adultos</h2>      
  12.   <table class="table">
  13.     <thead>
  14.       <tr>
  15.         <th>URL</th>
  16.         <th>Título</th>
  17.         <th>Data</th>
  18.       </tr>
  19.     </thead>
  20.     <tbody>
  21.    
  22.     <?php
  23.     // Informações de conexão ao banco de dados
  24.     $servername = "localhost";
  25.     $username = "root";
  26.     $password = "";
  27.     $dbname = "0000";
  28.  
  29.     // Conectando ao banco de dados usando PDO
  30.     try {
  31.       $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  32.       $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  33.     } catch(PDOException $e) {
  34.       echo "Falha na conexão: " . $e->getMessage();
  35.     }
  36.  
  37.     // Configurando a paginação
  38.     $results_per_page = 15;
  39.  
  40.     if (isset($_GET["page"])) {
  41.       $page = $_GET["page"];
  42.     } else {
  43.       $page = 1;
  44.     }
  45.  
  46.     $start_from = ($page-1) * $results_per_page;
  47.  
  48.     // Selecionando registros da tabela usando PDO
  49.     $stmt = $conn->prepare("SELECT url, title, date FROM adulto LIMIT :start_from, :results_per_page");
  50.     $stmt->bindParam(':start_from', $start_from, PDO::PARAM_INT);
  51.     $stmt->bindParam(':results_per_page', $results_per_page, PDO::PARAM_INT);
  52.     $stmt->execute();
  53.  
  54.     $result = $stmt->fetchAll();
  55.  
  56.     if ($result) {
  57.       // Exibindo os registros na tabela
  58.       foreach($result as $row) {
  59.         echo "<tr>";
  60.         echo "<td>" . $row["url"] . "</td>";
  61.         echo "<td>" . $row["title"] . "</td>";
  62.         echo "<td>" . $row["date"] . "</td>";
  63.         echo "</tr>";
  64.       }
  65.     } else {
  66.       echo "0 resultados";
  67.     }
  68.  
  69.     // Exibindo links para navegar pelas páginas
  70.     $stmt = $conn->prepare("SELECT COUNT(*) AS total FROM adulto");
  71.     $stmt->execute();
  72.  
  73.     $row = $stmt->fetch(PDO::FETCH_ASSOC);
  74.     $total_pages = ceil($row["total"] / $results_per_page);
  75.  
  76.     echo "<nav aria-label='Page navigation example'>";
  77.     echo "<ul class='pagination justify-content-center'>";
  78.  
  79.     $prev_class = ($page == 1) ? 'disabled' : '';
  80.     echo "<li class='page-item {$prev_class}'><a class='page-link' href='index.php?page=".($page-1)."'>&laquo;</a></li>";
  81.  
  82.     for ($i=1; $i<=$total_pages; $i++) {
  83.       $active_class = ($page == $i) ? 'active' : '';
  84.       echo "<li class='page-item {$active_class}'><a class='page-link' href='index.php?page=".$i."'>".$i."</a></li>";
  85.     }
  86.  
  87.     $next_class = ($page == $total_pages) ? 'disabled' : '';
  88. echo "<li class='page-item {$next_class}'><a class='page-link' href='index.php?page=".($page+1)."'>&raquo;</a></li>";
  89.  
  90. echo "</ul>";
  91. echo "</nav>";
  92.  
  93. // Encerrando a conexão com o banco de dados
  94. $conn = null;
  95. ?>
  96.  
  97. </tbody>
  98.  
  99. </table>
  100. </div>
  101. <!-- Bootstrap JS -->
  102. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js"></script>
  103. <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
  104. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
  105. </body>
  106. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement