PHP Auto Fit Range Page Paging program

Source: Internet
Author: User

Effect Chart:

Remember before I saw a page number way of paging effect, the number of pages more folding lines continue to show up to more than 60 pages, hehe! A little influence beautiful!

So I wrote a page-numbering program that automatically adapts to the range.

In other words, the page number format follows

<< < 1 2 3 4 5 6 7 > >>

<< < 6 7 8 9 all > >>

Underline is the current page, so you can display a similar page number, to avoid too long affect the appearance of

Code added a note, mainly novice to see, oh, write this function, did not consider the separation of performance structure, the master will not be prosecuted!

<?php
Function page ($page, $total, $phpfile, $pagesize =10, $pagelen =7) {
$pagecode = ';//define variable to hold page-generated HTML
$page = Intval ($page);//Avoid Non-numeric page numbers
$total = Intval ($total);//Guaranteed Total record value type is correct
if (! $total) return array ();//The total number of records is zero returns an empty array
$pages = Ceil ($total/$pagesize);//Calculate Total page
Handling Page number legality
if ($page <1) $page = 1;
if ($page > $pages) $page = $pages;
Calculate query Offsets
$offset = $pagesize * ($page-1);
Page range Calculation
$init = 1;//starting page number
$max = $pages;//End page number
$pagelen = ($pagelen%2)? $pagelen: $pagelen +1;//page number
$pageoffset = ($pagelen-1)/2;//the left and right offset of page number
Generate HTML
$pagecode = ' <div class= ' page ' > ';
$pagecode. = "<span> $page/$pages </span>";//The first few pages, altogether several pages
If this is the first page, the connection to the first and previous pages is not displayed
if ($page!=1) {
$pagecode. = "<a href=\" {$phpfile}?page=1\ ">&lt;&lt;</a>";//The first page
$pagecode. = "<a href=\" {$phpfile}?page=. ($page-1). " \ >&lt;</a>/Previous page
}
Can be offset when the number of pages is greater than page numbers
if ($pages > $pagelen) {
If the current page is less than or equal to the left offset
if ($page <= $pageoffset) {
$init = 1;
$max = $pagelen;
}else{//If the current page is greater than the left offset
If the current page number right offset exceeds the maximum page count
if ($page + $pageoffset >= $pages + 1) {
$init = $pages-$pagelen +1;
}else{
Computation of the left and right offsets when they exist
$init = $page-$pageoffset;
$max = $page + $pageoffset;
}
}
}
Generate HTML
for ($i = $init; $i <= $max; $i + +) {
if ($i = = $page) {
$pagecode. = ' <span> '. $i. ' </span> ';
} else {
$pagecode. = "<a href=\" {$phpfile}?page={$i}\ "> $i </a>";
}
}
if ($page!= $pages) {
$pagecode. = "<a href=\" {$phpfile}?page=. ($page + 1). " \ ">&gt;</a>";//Next page
$pagecode. = "<a href=\" {$phpfile}?page={$pages}\ ">&gt;&gt;</a>";//Last Page
}
$pagecode. = ' </div> ';
Return Array (' Pagecode ' => $pagecode, ' sqllimit ' => ' limit '. $offset ', '. $pagesize);
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.