It is famous function for pagination. CSS allows display links in more convenient form.
.page {
border:1px solid #BFBFBF;
font-weight: bold;
margin:2px;
padding:3px 5px;
text-decoration:none;
}
a.page:link {color:#505050;} /* unvisited link */
a.page:visited {color:#505050;} /* visited link */
a.page:hover {border-color:black;color:#2554C7;} /* mouse over link */
function pagination($total_pages,$page){
global $webpage;
$pagination = '<div class="page_numbers">';
if($total_pages!=1){
//the total links visible
$max_links=10;
//$max links_marker is the top of the loop
//$h is the start
$max_links_marker = $max_links+1;
$h=1;
//$link_block is the block of links on the page
//When this is an integer we need a new block of links
$link_block=(($page-1)/$max_links);
//if the page is greater than the top of th loop and link block
//is an integer
if(($page>=$max_links_marker)&&(is_int($link_block))){
//reset the top of the loop to a new link block
$max_links_marker=$page+$max_links;
//and set the bottom of the loop
$h=$max_links_marker-$max_links;
$prev=$h-1;
}
//if not an integer we are still within a link block
elseif(($page>=$max_links_marker)&&(!is_int($link_block))){
//round up the link block
$round_up=ceil($link_block);
$new_top_link = $round_up*$max_links;
//and set the top of the loop to the top link
$max_links_marker=$new_top_link+1;
//and the bottom of the loop to the top - max links
$h=$max_links_marker-$max_links;
$prev=$h-1;
}
//if greater than total pages then set the top of the loop to
// total_pages
if($max_links_marker>$total_pages){
$max_links_marker=$total_pages+1;
}
//first and prev buttons
if($page>'1'){
$pagination.='<a class="page" href="'.$webpage.'?page=1">First</a>
<a class="page" href="'.$webpage.'?page='.($page-1).'">Prev</a>';
}
//provide a link to the previous block of links
$prev_start = $h-$max_links;
$prev_end = $h-1;
if($prev_start <=1){
$prev_start=1;
}
$prev_block = "Pages $prev_start to $prev_end";
if($page>$max_links){
$pagination.='<a class="page" href="'.$webpage.'?page='.$prev.'">'.$prev_block.'</a>';
}
//loop through the results
for ($i=$h;$i<$max_links_marker;$i++){
if($i==$page){
$pagination.= '<a class="current">'.$i.'</a>';
}
else{
$pagination.= '<a class="page" href="'.$webpage.'?page='.$i.'">'.$i.'</a>';
}
}
//provide a link to the next block o links
$next_start = $max_links_marker;
$next_end = $max_links_marker+$max_links;
if($next_end >=$total_pages){
$next_end=$total_pages;
}
$next_block = "Pages $next_start to $next_end";
if($total_pages>$max_links_marker-1){
$pagination.='<a class="page" href="'.$webpage.'?page='.$max_links_marker.'">'.$next_block.'</a>';
}
//link to next and last pages
if(($page >="1")&&($page!=$total_pages)){
$pagination.='<a class="page" href="'.$webpage.'?page='.($page+1).'">Next</a>
<a class="page" href="'.$webpage.'?page='.$total_pages.'">Last</a>';
}
}
//if one page of results
else{
$pagination.='<a class="page" href="" class="current">1</a>';
}
$pagination.='
</div>';
return($pagination);
}
Немає коментарів:
Дописати коментар