Skip to content

Instantly share code, notes, and snippets.

@wesamly
Created August 6, 2017 08:27
Show Gist options
  • Save wesamly/6669f93e3550a88f69a496f56c3f442c to your computer and use it in GitHub Desktop.
Save wesamly/6669f93e3550a88f69a496f56c3f442c to your computer and use it in GitHub Desktop.
Limit Long Pagination List
<?php
$pages = 22;
$current = 1;
if (isset($_GET['p'])) {
$current = filter_var($_GET['p'], FILTER_SANITIZE_NUMBER_INT);
}
$maxShow = 10;
$start = 1;
$end = $pages;
if ($pages > $maxShow) {
$beforeCurrent = ceil($maxShow/2);
$start = $current - $beforeCurrent;
if ($start < 1) {
$start = 1;
}
$end = $start + $maxShow - 1;
if ($end > $pages) {
$end = $pages;
$start = $end - $maxShow + 1;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test Limit Pagination</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Test Limit Pagination</h1>
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php if($start > 1) { ?>
<li>
<span aria-hidden="true">...</span>
</li>
<?php } ?>
<?php
for ($i = $start; $i <= $end; $i++) {
?>
<li <?php if ($current == $i) { ?>class="active"<?php } ?>><a href="?p=<?php echo $i; ?>"><?php echo $i; ?></a></li>
<?php
}
?>
<?php if($end < $pages) { ?>
<li>
<span aria-hidden="true">...</span>
</li>
<?php } ?>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment