Skip to content

Instantly share code, notes, and snippets.

@mesbahulalam
Created November 20, 2019 15:18
Show Gist options
  • Save mesbahulalam/b98bcac0cd38ab3bf0ddd0a4069586f5 to your computer and use it in GitHub Desktop.
Save mesbahulalam/b98bcac0cd38ab3bf0ddd0a4069586f5 to your computer and use it in GitHub Desktop.
<?php
$data = file("data");
$total = count($data);
$limit = 10;
$page = intval($_GET["page"]);
if($page<=0) $page = 1;
$pages = ($total) ? ceil($total/$limit) : 1;
$count = 0;
$i = ($page-1)*$limit;
while($count<$limit && $i<$total){
echo $data[$i];
$i++;
$count++;
}
echo pagination($total, $page, false, $limit);
function pagination($total, $current, $format = "?page=%d", $limit='1'){
$page_count = ceil($total/$limit);
$current_range = array(($current-5 < 1 ? 1 : $current-3), ($current+5 > $page_count ? $page_count : $current+3));
$first_page = $current > 5 ? '<li><a href="'.sprintf($format, '1').'">First</a></li>'.($current < 5 ? ' ' : '') : null;
$last_page = $current < $page_count-2 ? ($current > $page_count-4 ? ' ' : ' ').'<li><a href="'.sprintf($format, $page_count).'">Last</a></li>' : null;
$previous_page = $current > 1 ? '<li><a href="'.sprintf($format, ($current-1)).'">Previous</a></li> ' : null;
$next_page = $current < $page_count ? ' <li><a href="'.sprintf($format, ($current+1)).'">Next</a></li> ' : null;
for ($x=$current_range[0];$x <= $current_range[1]; ++$x)
$pages[] = ($x == $current ? '<li class="active"><a href="#">'.$x.'</a></li>' : '<li><a href="'.sprintf($format, $x).'"">'.$x.'</a></li>');
if ($page_count > 1)
return '<center><ul class="pagination">'.$first_page.$previous_page.implode(' ', $pages).$next_page.$last_page.'</ul></center>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment