Created
August 6, 2017 08:27
-
-
Save wesamly/6669f93e3550a88f69a496f56c3f442c to your computer and use it in GitHub Desktop.
Limit Long Pagination List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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">«</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">»</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