1
2<html>
3<head>
4 <title>Pagination</title>
5 <!-- Bootstrap CDN -->
6 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
8 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
9</head>
10<body>
11 <?php
12
13 if (isset($_GET['pageno'])) {
14 $pageno = $_GET['pageno'];
15 } else {
16 $pageno = 1;
17 }
18 $no_of_records_per_page = 10;
19 $offset = ($pageno-1) * $no_of_records_per_page;
20
21 $conn=mysqli_connect("localhost","my_user","my_password","my_db");
22 // Check connection
23 if (mysqli_connect_errno()){
24 echo "Failed to connect to MySQL: " . mysqli_connect_error();
25 die();
26 }
27
28 $total_pages_sql = "SELECT COUNT(*) FROM table";
29 $result = mysqli_query($conn,$total_pages_sql);
30 $total_rows = mysqli_fetch_array($result)[0];
31 $total_pages = ceil($total_rows / $no_of_records_per_page);
32
33 $sql = "SELECT * FROM table LIMIT $offset, $no_of_records_per_page";
34 $res_data = mysqli_query($conn,$sql);
35 while($row = mysqli_fetch_array($res_data)){
36 //here goes the data
37 }
38 mysqli_close($conn);
39 ?>
40 <ul class="pagination">
41 <li><a href="?pageno=1">First</a></li>
42 <li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
43 <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
44 </li>
45 <li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
46 <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a>
47 </li>
48 <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
49 </ul>
50</body>
51</html>
52
1try {
2
3 // Find out how many items are in the table
4 $total = $dbh->query('
5 SELECT
6 COUNT(*)
7 FROM
8 table
9 ')->fetchColumn();
10
11 // How many items to list per page
12 $limit = 20;
13
14 // How many pages will there be
15 $pages = ceil($total / $limit);
16
17 // What page are we currently on?
18 $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
19 'options' => array(
20 'default' => 1,
21 'min_range' => 1,
22 ),
23 )));
24
25 // Calculate the offset for the query
26 $offset = ($page - 1) * $limit;
27
28 // Some information to display to the user
29 $start = $offset + 1;
30 $end = min(($offset + $limit), $total);
31
32 // The "back" link
33 $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
34
35 // The "forward" link
36 $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';
37
38 // Display the paging information
39 echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';
40
41 // Prepare the paged query
42 $stmt = $dbh->prepare('
43 SELECT
44 *
45 FROM
46 table
47 ORDER BY
48 name
49 LIMIT
50 :limit
51 OFFSET
52 :offset
53 ');
54
55 // Bind the query params
56 $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
57 $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
58 $stmt->execute();
59
60 // Do we have any results?
61 if ($stmt->rowCount() > 0) {
62 // Define how we want to fetch the results
63 $stmt->setFetchMode(PDO::FETCH_ASSOC);
64 $iterator = new IteratorIterator($stmt);
65
66 // Display the results
67 foreach ($iterator as $row) {
68 echo '<p>', $row['name'], '</p>';
69 }
70
71 } else {
72 echo '<p>No results could be displayed.</p>';
73 }
74
75} catch (Exception $e) {
76 echo '<p>', $e->getMessage(), '</p>';
77}