Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 05:04
Show Gist options
  • Save code-boxx/678f2ad6768456c1937bfe0410f3b781 to your computer and use it in GitHub Desktop.
Save code-boxx/678f2ad6768456c1937bfe0410f3b781 to your computer and use it in GitHub Desktop.
PHP MYSQL Pagination Example

PHP MYSQL PAGINATION EXAMPLE

https://code-boxx.com/simple-php-mysql-pagination/

NOTES

  1. Create a test database and import x-dummy.sql.
  2. Change the settings in 1-paginate.php to your own and launch it in the browser

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) SETTINGS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("PER_PAGE", "10"); // entries per page
// (B) CONNECT TO DATABASE
$pdo = new PDO(
"mysql:host=". DB_HOST .";dbname=". DB_NAME .";charset=". DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// (C) TOTAL NUMBER OF PAGES
$stmt = $pdo->prepare("SELECT CEILING(COUNT(*) / ".PER_PAGE.") `pages` FROM `users`");
$stmt->execute();
$pageTotal = $stmt->fetchColumn();
// (D) GET ENTRIES FOR CURRENT PAGE
// (D1) LIMIT (X, Y) FOR SQL QUERY
$pageNow = isset($_GET["page"]) ? $_GET["page"] : 1 ;
$limX = ($pageNow - 1) * PER_PAGE;
$limY = PER_PAGE;
// (D2) SQL FETCH
$stmt = $pdo->prepare("SELECT * FROM `users` ORDER BY `id` LIMIT $limX, $limY");
$stmt->execute();
$users = $stmt->fetchAll();
// (E) OUTPUT HTML ?>
<!DOCTYPE html>
<html>
<head>
<title>Pagination Example</title>
<meta charset="utf-8">
<link rel="stylesheet" href="2-paginate.css">
</head>
<body>
<!-- (E1) USER LIST -->
<ul class="demo"><?php
foreach ($users as $u) {
printf("<li>%u %s</li>", $u["id"], $u["name"]);
}
?></ul>
<!-- (E2) PAGINATION -->
<div class="pagination"><?php
for ($i=1; $i<=$pageTotal; $i++) {
printf("<a %shref='1-paginate.php?page=%u'>%u</a>",
$i==$pageNow ? "class='current' " : "", $i, $i
);
}
?></div>
</body>
</html>
/* (A) PAGINATION SQUARES */
.pagination a, .pagination div {
display: inline-block;
text-decoration: none;
color: #333;
padding: 10px;
border: 1px solid #e1e1e1;
margin: 5px;
background: #f7f7f7;
}
/* (B) ON HOVER */
.pagination a:hover, .pagination div:hover {
cursor: pointer;
color: #fff;
background: #4680db;
}
/* (C) HIGHLIGHT CURRENT PAGE */
.pagination a.current, .pagination div.current {
font-weight: bold;
color: #000;
background: #fffc9b;
}
/* (X) NOT IMPORTANT */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
.demo {
width: 300px;
list-style: none;
padding: 0;
margin: 0 0 10px 0;
}
.demo li {
padding: 10px;
border: 1px solid #efefef;
background: #f2f2f2;
}
.demo li:nth-child(even) {
background: #fff;
}
CREATE TABLE `users` (
`id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `users`
ADD PRIMARY KEY (`id`),
ADD KEY `name` (`name`);
ALTER TABLE `users`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;
INSERT INTO `users` (`id`, `name`) VALUES
(24, 'Abby Ward'),
(21, 'Aleksandra Devine'),
(47, 'Aston Kramer'),
(43, 'Aston Simmonds'),
(15, 'Beth Skywalker'),
(26, 'Bridget Wooten'),
(10, 'Coby Kelleigh'),
(20, 'Darrah Shadow'),
(9, 'Drake Adelaide'),
(12, 'Elizabeth Stewart'),
(19, 'Gareth Solderini'),
(46, 'Gregor Bryant'),
(42, 'Gregor Hollis'),
(13, 'Hannah Strickland'),
(52, 'Harvey Frame'),
(7, 'Imogene Thad'),
(53, 'Ismaeel Carty'),
(45, 'Issac Burris'),
(41, 'Issac Calderon'),
(2, 'Jane Doe'),
(5, 'Jaslyn Keely'),
(40, 'Jax Howe'),
(39, 'Joey Whyte'),
(1, 'John Doe'),
(17, 'Joseph Stewart'),
(34, 'Julia Greaves'),
(31, 'Junior Douglas'),
(32, 'Kaiden Bentley'),
(16, 'Kenneth Sanders'),
(30, 'Keziah Knapp'),
(28, 'Kirstie Thomas'),
(33, 'Lawrence Murphy'),
(14, 'Leah Shan'),
(51, 'Marcus Best'),
(29, 'Maya Paine'),
(23, 'Myla Bostock'),
(50, 'Nathaniel Khan'),
(4, 'Peers Sera'),
(6, 'Richard Breann'),
(54, 'Rowan Avalos'),
(3, 'Rusty Terry'),
(37, 'Sacha Gross'),
(27, 'Sally Castillo'),
(11, 'Sarah Sanders'),
(18, 'Seth Sonnel'),
(38, 'Shannon Peterson'),
(25, 'Shayan Clements'),
(49, 'Shoaib Vickers'),
(35, 'Sulaiman Gilmour'),
(44, 'Taran Morin'),
(48, 'Taran Sampson'),
(22, 'Thelma Kim'),
(8, 'Tillie Sharalyn'),
(36, 'Virgil Collier');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment