PHP Auto-Fit Range page numbering Program _php tutorial

Source: Internet
Author: User


Remember to see a page numbering method of the pagination effect, the number of pages more than the fold line continues to display until more than 60 pages, hehe! A little influence on beauty!

So I wrote a page-paging program that can automatically adapt to the range.
That is, page numbers are formatted as follows
<< < 1 2 3 4 5 6 7 > >>
<< < 6 7 8 9 > >>

Underline the current page so that a similar page number can be fixed to avoid too long an effect on aesthetics
Code added comments, mainly novice look, hehe, write this function, no consideration of the performance structure separation problem, Master will not be investigated!
Function page ($page, $total, $phpfile, $pagesize =10, $pagelen =7) {
$pagecode = ";//define variables to store 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);//Calculation of total pages
Handling page numbering legality
if ($page <1) $page = 1;
if ($page > $pages) $page = $pages;
Calculate Query Offset
$offset = $pagesize * ($page-1);
Page range Calculation
$init = 1;//Start Page number
$max = $pages;//End page number
$pagelen = ($pagelen%2)? $pagelen: $pagelen +1;//page number
$pageoffset = ($pagelen-1)/2;//page number offset

Generate HTML
$pagecode = ";
$pagecode. = "$page/$pages";///page, total pages
If this is the first page, the connection to the first and previous pages is not displayed
if ($page!=1) {
$pagecode. = "<<";//First page
$pagecode. = "<";//prev
}
Can be offset when the number of pages is greater than the number
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 right offset of the current page number exceeds the Max page count
if ($page + $pageoffset >= $pages + 1) {
$init = $pages-$pagelen +1;
}else{
Calculations when left and right offsets are present
$init = $page-$pageoffset;
$max = $page + $pageoffset;
}
}
}
Generate HTML
for ($i = $init; $i <= $max; $i + +) {
if ($i = = $page) {
$pagecode. = ''. $i. '';
} else {
$pagecode. = "$i";
}
}
if ($page! = $pages) {
$pagecode. = ">";//Next page
$pagecode. = ">>";//Last Page
}
$pagecode. = ';
Return Array (' pagecode ' = $pagecode, ' sqllimit ' = ' limit '. $offset. ', ' $pagesize);
}
?>


Function parameters:

$page the page number that the current $_get obtains
Total number of $total records
$phpfile page number Connection file name
$pagesize don't have to explain, huh?
$pagelen show a few page numbers note (odd), symmetrical!
The function returns an array:
The content of the Pagecode index is the generated HTML code
The Sqllimit index corresponds to the SQL limit suffix

How to use:

$phpfile = ' index.php ';
$page = isset ($_get[' page ')? $_get[' page ']:1;
$db = new Mysql ();
$counts = $db->counts (' Select ' id ' from ' test ');
$sql = ' select ' id ', ' title ' From ' Test ';
$getpageinfo = page ($page, $counts, $phpfile);
$sql. = $getpageinfo [' Sqllimit '];
$data = $db->getrows ($sql);
?>
echo $getpageinfo [' Pagecode '];//HTML code showing pagination
?>

Css

    • Total 2 Pages:
    • Previous page
    • 1
    • 2
    • Next page

http://www.bkjia.com/PHPjc/363989.html www.bkjia.com true http://www.bkjia.com/PHPjc/363989.html techarticle : Remember to see a page numbering method of the pagination effect, the number of pages more than the fold line continues to display until more than 60 pages, hehe! A little influence on beauty! Then wrote a, can from ...

  • Related Article

    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.