Last active
January 8, 2019 20:59
-
-
Save arnaudjuracek/04053e8d851cff0151e33244888e1bb2 to your computer and use it in GitHub Desktop.
basic-pagination.php
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
<ul class="items"> | |
<?php | |
$options = array( | |
'quantity' => 20, // how many item to display for each page | |
'around' => 2, // how many page btn to show around the current page btn | |
'directory' => 'files/tbn-test', // dir to scan for items | |
); | |
$page = isset($_GET['page']) ? $_GET['page'] : 1; | |
$offset = ($page - 1) * $options['quantity']; // $page base index is 1 | |
$filelist = scandir($options['directory']); | |
//get subset of file array | |
$selectedFiles = array_slice($filelist, $offset, $options['quantity']); | |
foreach ($selectedFiles as $file) { | |
$path = $options['directory'] . '/' . $file; | |
if (is_array(getimagesize($path))) { | |
echo '<li class="item"><img src="'. $path .'"></li>'; | |
} | |
} | |
?> | |
</ul> | |
<div class="pagination"> | |
<a class="btn" <?= $page <= 1 ? 'disabled' : '' ?> href="?page=<?= $page - 1 ?>">←</a> | |
<?php | |
$len = count($filelist) / $options['quantity']; | |
for ($i = 1; $i < $len + 1; $i++) { | |
if (($i == 1 || $i > $len) || ($i > $page - $options['around'] && $i < $page + $options['around'])) { | |
echo '<a class="btn '. ($page == $i ? 'active' : '') .'" href="?page='.$i.'">'. $i .'</a>'; | |
} elseif ($i > $page - $options['around'] - 1 && $i < $page + $options['around'] + 1) { | |
echo '<a disabled class="btn">…</a>'; | |
} | |
} | |
?> | |
<a class="btn" <?= $page >= $len ? 'disabled' : '' ?> href="?page=<?= $page + 1 ?>">→</a> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment