phpist

Untitled

Mar 10th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. <?php
  2. $mysqli = new mysqli("localhost", "admin", "admin", "pagination") or die ("Error");
  3. if(!isset($_GET['page'])) $page = 1; else $page = htmlspecialchars($_GET['page']);
  4. if(ctype_digit($page) === false) $page = 1;
  5.  
  6.  
  7. $table = "items";
  8. $count_query = $mysqli->query("SELECT COUNT(*) FROM $table");
  9. $count_array = $count_query->fetch_array(MYSQLI_NUM);
  10. $count = $count_array[0];
  11. $limit = 10;
  12. $start = ($page*$limit)-$limit;
  13. $length = ceil($count/$limit);
  14.  
  15. if((int)$page > $length || $page <= 0) $start = 0;
  16. $query = $mysqli->query("SELECT * FROM $table ORDER BY id DESC LIMIT $start, $limit ");
  17.  
  18. function Pagination($length, $page){
  19.  
  20.  
  21. if($length < 5) foreach(range(1, $length) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
  22.  
  23. if($length > 4 && $page < 5) foreach(range(1, 5) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
  24.  
  25. if($length - 5 < 5 && $page > 5 && $length - 5 > 0) foreach(range($length - 4, $length) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
  26.  
  27. if($length > 4 && $length - 5 < 5 && $page == 5) foreach(range($page-2, $length) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
  28.  
  29. if($length > 4 && $length-5 > 5 && $page >=5 && $page <= $length-4) foreach(range($page-2, $page+2) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
  30.  
  31. if($length > 4 && $length-5 > 5 && $page > $length-4) foreach(range($length-4, $length) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
  32. }
  33.  
  34. ?>
  35. <!doctype html>
  36. <html>
  37. <head>
  38. <meta charset="utf-8">
  39. <title>Pagination</title>
  40. <link rel="stylesheet" type="text/css" href="style.css">
  41. </head>
  42.  
  43. <body>
  44. <?php
  45. Pagination($length, $page);
  46. while($row = $query->fetch_assoc()) echo '<h1>'.$row["id"].' '.$row["data"].''.($row["summa"]).'</h1>';
  47.  
  48. Pagination($length, $page);
  49. ?>
  50. </body>
  51. </html>
Add Comment
Please, Sign In to add comment