PHP Universal Paging implementation

Source: Internet
Author: User

Write a General page of your own, with the Drop-down selection, you can set the number of page links to display, not to say, look at the code and comments:

1<?php
2//written by Shaoyun 20:17 May 31, 2008
3//email:shaoyun at Yeah.net
4//blog:http://shaoyun.cnblogs.com
5//Paging function: Splitpage (total number of records, number of records per page, display several page links)
6function splitpage ($rscount, $pagesize, $shownum) {
7//Calculate Total Pages
8if ($rscount% $pagesize) {
9 $pagenum = (int) ($rscount/$pagesize) +1;
10}else{
One $pagenum = (int) ($rscount/$pagesize);
12}
13//filter Get the correct page number
14if (!isset ($_get["page")) {
$curpage = 1;
16}elseif (Is_numeric ($_get["page")) {
if ($_get["page"]> $pagenum)
$curpage = $pagenum;
Else
$curpage = (int) $_get["page"];
if ($curpage <1)
$curpage = 1;
23}else{
$curpage = 1;
25}
26//set previous page, next page, previous page, down page
27$first_page=1;
28$last_page= $pagenum;
29$up_page= $curpage-1;
30$down_page= $curpage +1;
31//get the current file name
32$addr_url=$_server["Script_name"];
33$addr_url=strrev ($addr _url);
34$addr_url=substr ($addr _url,0,strpos ($addr _url, "/"));
35$addr_url=strrev ($addr _url);
36$addr_url= $addr _url. "? Page= ";
37
38$page_html= ' altogether '. $rscount. ' Record &nbsp; '. $pagesize. ' article/Page &nbsp; '. $curpage. ' /'. $pagenum. ' Page &nbsp; ';
39
40$page_html.= "<a href= '". $addr _url. $first _page. "' > Home </a>&nbsp; ";
41if ($curpage >1) {
$page _html.= "<a href=". $addr _url. $up _page. "' > Prev </a>&nbsp; ";
43}else{
$page _html.= "Prev &nbsp;";
45}
46//Compute the paging boundary
47if ($shownum% 2) {
$show _front_num= (int) ($shownum/2);
$show _back_num= (int) ($shownum/2);
50}else{
Wuyi $show _front_num= (int) ($shownum/2)-1;
$show _back_num= (int) ($shownum/2);
53}
54if ($curpage < $show _front_num+1) {
$fbegin = 1;
$fend = $shownum;
57}elseif ($curpage + $show _back_num> $pagenum) {
$fend = $pagenum;
$fbegin = $pagenum-$shownum +1;
60}else{
$fbegin = $curpage-$show _front_num;
$fend = $curpage + $show _back_num;
63}
64//pagination Link
65for ($idx = $fbegin; $idx < $fend +1;++ $idx) {
if ($curpage = = $idx) {
$page _html.= "<b>". $idx. " </b>&nbsp; ";
}else{
$page _html.= "<a href=". $addr _url. $idx. "' > ". $idx." </a>&nbsp; ";
70}
71}
72
73if ($curpage < $pagenum) {
$page _html.= "<a href=". $addr _url. $down _page. "' > next page </a>&nbsp; ";
75}else{
$page _html.= "next page &nbsp;";
77}
78$page_html.= "<a href= '". $addr _url. $last _page. "' > Last </a>&nbsp; ";
79//Dropdown Navigation Code
80$page_html.= "Jump to &nbsp;";
81$page_html.= "<select onchange=window.location.href=this.options[this.selectedindex].value>";
82for ($idx =1 $idx < $pagenum +1;++ $idx) {
if ($curpage = = $idx) {
$page _html.= "<option value=". $addr _url. $idx. "' Selected>, $idx." Page </option> ";
}else{
$page _html.= "<option value= '". $addr _url. $idx. "' > ". $idx." Page </option> ";
87}
88}
89$page_html.= "</select>";
90
91echo $page _html;
92}
93?>

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.