Advertisement
MizunoBrasil

Paginação simples com Conexão Via PDO

Dec 19th, 2022
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2.     session_start();
  3.     include('converte_data.php');
  4.  
  5.     //As tres linhas abaixo são a paginação
  6.     $pagina = 1;
  7.  
  8.     if (isset($_GET['pagina']))
  9.         $pagina = filter_input(INPUT_GET, "pagina", FILTER_VALIDATE_INT);
  10.  
  11.     if (!$pagina)
  12.         $pagina = 1;
  13.  
  14.     $limite = 4;
  15.  
  16.     $inicio = ($pagina * $limite) - $limite;
  17.  
  18.     $con = new PDO("mysql:host=localhost;dbname=album","root","");
  19.  
  20.     $registros = $con->query("SELECT COUNT(nome) count FROM imagem")->fetch()["count"];
  21.  
  22.     $paginas = ceil($registros / $limite);
  23.    
  24.     $result = $con->query("SELECT nome FROM imagem ORDER BY nome LIMIT $inicio, $limite")->fetchall();
  25.  
  26.     //var_dump($result);
  27.  
  28.     ?>
  29. <!DOCTYPE html>
  30. <html lang="pt-br">
  31.     <head>
  32.         <meta charset="UTF-8">
  33.         <meta http-equiv="X-UA-Compatible" content="IE=edge">
  34.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  35.         <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  36.         <link rel="preconnect" href="https://fonts.googleapis.com">
  37.         <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  38.         <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
  39.         <link rel="stylesheet" href="css/style.css">
  40.         <title>Galeria</title>
  41.     </head>
  42.     <style>
  43.     body {
  44.     font-family: 'Poppins', sans-serif;
  45.     font-size: 15px;
  46.     }
  47.     </style>
  48.     <body>
  49.         <br><br>
  50.         <div class="container">
  51.             <ul>
  52.                 <?php foreach ($result as $item): ?>
  53.                     <li><?=$item["nome"]?></li>
  54.                 <?php endforeach; ?>
  55.             </ul>
  56.  
  57.  
  58.             <a href="?pagina=1">Primeira</a>
  59.  
  60.             <?php if ($pagina>1): ?>
  61.             <a href="?pagina=<?=$pagina-1?>"><<</a>
  62.             <?php endif; ?>
  63.  
  64.             <?=$pagina?>
  65.  
  66.             <?php if ($pagina<$paginas): ?>
  67.                 <a href="?pagina=<?=$pagina+1?>">>></a>
  68.  
  69.             <?php endif; ?>
  70.                
  71.             <a href="?pagina=<?=$paginas?>">Última</a>
  72.            
  73.        
  74.  
  75.         </container>
  76.     </body>
  77. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement