Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $mysqli = new mysqli("localhost", "admin", "admin", "pagination") or die ("Error");
- if(!isset($_GET['page'])) $page = 1; else $page = htmlspecialchars($_GET['page']);
- if(ctype_digit($page) === false) $page = 1;
- $table = "items";
- $count_query = $mysqli->query("SELECT COUNT(*) FROM $table");
- $count_array = $count_query->fetch_array(MYSQLI_NUM);
- $count = $count_array[0];
- $limit = 10;
- $start = ($page*$limit)-$limit;
- $length = ceil($count/$limit);
- $count_query1 = $mysqli->query("SELECT SUM(summa) as sum FROM $table");
- $count_array1 = $count_query1->fetch_array(MYSQLI_NUM);
- $count1 = $count_array1[0];
- $limit1 = 10;
- $start1 = ($page*$limit1)-$limit1;
- $length1 = ceil($count1/$limit1);
- if((int)$page > $length || $page <= 0) $start = 0;
- $query = $mysqli->query("SELECT * FROM $table ORDER BY id DESC LIMIT $start, $limit ");
- $query1 = $mysqli->query("SELECT SUM(summa) as sum FROM $table ORDER BY id LIMIT $start1, $limit1 ");
- function Pagination($length, $page){
- if($length < 5) foreach(range(1, $length) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
- if($length > 4 && $page < 5) foreach(range(1, 5) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
- 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> ';
- if($length > 4 && $length - 5 < 5 && $page == 5) foreach(range($page-2, $length) as $p) echo '<a href="index.php?page='.$p.'">'.$p.'</a> ';
- 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> ';
- 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> ';
- }
- ?>
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Pagination</title>
- <link rel="stylesheet" type="text/css" href="style.css">
- </head>
- <body>
- <?php
- Pagination($length, $page);
- while($row = $query->fetch_assoc()) echo '<h1>'.$row["id"].' '.$row["data"].''.($row["summa"]).'</h1>';
- while($row1 = $query1->fetch_assoc()) echo '<h1>'.($row1["sum"]).'</h1>';
- Pagination($length, $page);
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment