phpist

Untitled

Mar 11th, 2020
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 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.  
  16. $count_query1 = $mysqli->query("SELECT SUM(summa) as sum FROM $table");
  17. $count_array1 = $count_query1->fetch_array(MYSQLI_NUM);
  18. $count1 = $count_array1[0];
  19. $limit1 = 10;
  20. $start1 = ($page*$limit1)-$limit1;
  21. $length1 = ceil($count1/$limit1);
  22.  
  23. if((int)$page > $length || $page <= 0) $start = 0;
  24. $query = $mysqli->query("SELECT * FROM $table ORDER BY id DESC LIMIT $start, $limit ");
  25. $query1 = $mysqli->query("SELECT SUM(summa) as sum FROM $table ORDER BY id LIMIT $start1, $limit1 ");
  26.  
  27. function Pagination($length, $page){
  28.  
  29.  
  30. if($length < 5) foreach(range(1, $length) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
  31.  
  32. if($length > 4 && $page < 5) foreach(range(1, 5) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
  33.  
  34. 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> ';
  35.  
  36. if($length > 4 && $length - 5 < 5 && $page == 5) foreach(range($page-2, $length) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
  37.  
  38. 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> ';
  39.  
  40. 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> ';
  41. }
  42.  
  43. ?>
  44. <!doctype html>
  45. <html>
  46. <head>
  47. <meta charset="utf-8">
  48. <title>Pagination</title>
  49. <link rel="stylesheet" type="text/css" href="style.css">
  50. </head>
  51.  
  52. <body>
  53. <?php
  54. Pagination($length, $page);
  55. while($row = $query->fetch_assoc()) echo '<h1>'.$row["id"].' '.$row["data"].''.($row["summa"]).'</h1>';
  56. while($row1 = $query1->fetch_assoc()) echo '<h1>'.($row1["sum"]).'</h1>';
  57. Pagination($length, $page);
  58. ?>
  59. </body>
  60. </html>
Advertisement
Add Comment
Please, Sign In to add comment