Skip to content

Instantly share code, notes, and snippets.

@yumyo
Created January 26, 2017 15:51
Show Gist options
  • Save yumyo/4244058049c70794574c8bbe770a28a2 to your computer and use it in GitHub Desktop.
Save yumyo/4244058049c70794574c8bbe770a28a2 to your computer and use it in GitHub Desktop.
<?php
$items = array(1,2,3,4,5,6,7,8,9,10,11,12);
$limit = 5;
$qty_items = count($items);
$qty_pages = ceil($qty_items / $limit);
$curr_page = isset($_GET&#91;'page'&#93;) ? $_GET&#91;'page'&#93; : 1;
$next_page = $curr_page < $qty_pages ? $curr_page + 1 : null;
$prev_page = $curr_page > 1 ? $curr_page - 1 : null;
$offset = ($curr_page - 1) * $limit;
$items = array_slice($items, $offset, $limit);
?>
<style>
.curr{
border:1px solid #ddd;
padding:3px;
}
</style>
<ul>
<? foreach($items as $item): ?>
<li><?= $item ?></li>
<? endforeach ?>
</ul>
<? if($prev_page): ?>
<a href="pager.php?page=<?= $prev_page ?>"> << </a>
<? endif ?>
<? for($i = 1; $i <= $qty_pages; $i++): ?>
<a href="pager.php?page=<?= $i ?>" class="<?= ($i == $curr_page) ? 'curr' : '' ?>"><?= $i ?></a>
<? endfor ?>
<? if($next_page): ?>
<a href="pager.php?page=<?= $next_page ?>"> >> </a>
<? endif ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment