Page numbering automatically adapts to a range of paging codes

Source: Internet
Author: User
Tags array range
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 45 6 7 > >>
<< < 6 7 8 9> >>
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! copy content to clipboard

Code:

<?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\ "><<</a>";//The first page
$pagecode. = "<a href=\" {$phpfile}?page=. ($page-1). " \ ><</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). " \ ">></a>";//Next page
$pagecode. = "<a href=\" {$phpfile}?page={$pages}\ ">>></a>";//Last Page
}
$pagecode. = ' </div> ';
Return Array (' Pagecode ' => $pagecode, ' sqllimit ' => ' limit '. $offset ', '. $pagesize);
}
?>
Function arguments:
$page the page number obtained by the current $_get
Total number of $total records
$phpfile page number Connection file name
$pagesize don't have to explain, huh?
$pagelen display a maximum of several page numbers notice (odd number), symmetry!
function to return an array:
The corresponding content of the Pagecode index is the generated HTML code
The Sqllimit index corresponds to the SQL limit suffix

How to use: copy content to clipboard

Code:

<?php
$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);
?>
<?php
echo $getpageinfo['pagecode'];//显示分页的html代码
?>
Subsidized CSS copy content to clipboard

Code:

<style type="text/css">
body{font-family:Tahoma;}
.page{padding:2px;font-weight:bolder;font-size:12px;}
.page a{border:1px solid #ccc;padding:0 5px 0 5px;margin:2px;text-decoration:none;color:#333;}
.page span{padding:0 5px 0 5px;margin:2px;background:#09f;color:#fff;border:1px solid #09c;}
</style>
以下是HTML网页特效代码,点击运行按钮可查看效果:
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

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.