PHP page number form paging function supports static address and Ajax paging _php tips

Source: Internet
Author: User
Tags prev
Before each encounter pagination, always have to write their own, feel very cumbersome, so in accordance with the principle of general, wrote a pagination method, hereby record.

This page currently supports the Ajax paging (but JS has to write itself) for static address paging and no link addresses:

The supported static addresses are as follows: www.example.com/xxx-xxx-p1-xxxx-xx.html

Other forms of statics need to be rewritten according to their own circumstances

AJAX-enabled paging, the $link parameter is NULL, but provides PID and Optype, where PID is used to get the page number, and optype to distinguish between the current trigger action and which paging logic is used when a page has multiple paging
Copy Code code as follows:

/**********************************************************
*
* Get page number
*
**********************************************************
*
* @params string $link link address (you can flip it with Ajax when the link is empty)
*
* @params int $intPage Current page
*
* @params int $intTotal Total pages
*
* @params int $intSize number of pages to display
*
* @params string $type chain inoculation class (multiple paging for distinguishing page-flipping area)
*
**********************************************************
*
* @return Array
*/
Private Function formatpage ($link = "", $intPage, $intTotal, $intSize =3, $type = "")
{
$strPage = ' <div class= ' g_serpage clearfix ' > ';
if ($intTotal > 0)
{
if ($intPage > 1)
$strPage. = $link!= '? ' <a href= "'. Preg_replace ("/-p (\d+)/"," P ". ( $intPage-1), $link). ' " ><< prev </a> ': ' <a optype= '. $type. ' pid= ' Showpage_ '. ($intPage-1). ' " href= "javascript:void (0)" ><< prev </a> ';
Else
$strPage. = ' <a href= ' javascript:void (0) "><< prev </a>";
Window width is greater than or equal to total pages
if (($intSize +2) >= $intTotal)
{
for ($i =1; $i <= $intTotal; $i + +)
{
$strClass = $i = = $intPage? ' class= ' g_serpagcur ': ';
$strPage. = $link!= '? ' <a href= "'. Preg_replace ("/-p (\d+)/"," P ". $i, $link). ' '. $strClass. ' > ' $i. ' </a> ': ' <a optype= '. $type. ' pid= ' Showpage_ '. $i. ' href= ' javascript:void (0) "'. $strClass. ' > '. $i. ' </a> ';
}
}
Else
{
if ($intPage < Ceil ($intSize/2))
{
for ($i =1; $i <= $intSize; $i + +)
{
$strClass = $i = = $intPage? ' class= ' g_serpagcur ': ';
$strPage. = $link!= '? ' <a href= "'. Preg_replace ("/-p (\d+)/"," P ". $i, $link). ' '. $strClass. ' > ' $i. ' </a> ': ' <a optype= '. $type. ' pid= ' Showpage_ '. $i. ' href= ' javascript:void (0) "'. $strClass. ' > '. $i. ' </a> ';
}
$strPage. = $link!= '? ' <a class= "Gpage_nobor" >...</a><a href= "Preg_replace (/-p)/", "P". $intTotal, $link). ' > '. $ Inttotal. ' </a> ': ' <a class= ' Gpage_nobor ' >...</a><a optype= '. $type. ' pid= ' Showpage_ '. $intTotal. ' href= ' Javascript:void (0) ">". $intTotal. ' </a> ';
}
ElseIf ($intTotal-$intPage) < Ceil ($intSize/2))
{
$strPage. = $link!= '? ' <a href= "'. Preg_replace ("/-p (\d+)/"," P1 ", $link)." >1</a><a class= "Gpage_nobor" >...</a> ': ' <a optype= '. $type. ' pid= ' showpage_1 ' href= ' Javascript:void (0) ">1</a><a class=" Gpage_nobor ">...</a>";
for ($i = ($intTotal + 1-$intSize); $i + +; $i <= $intTotal)
{
$strClass = $i = = $intPage? ' class= ' g_serpagcur ': ';
$strPage. = $link!= '? ' <a href= "'. Preg_replace ("/-p (\d+)/"," P ". $i, $link). ' '. $strClass. ' > ' $i. ' </a> ': ' <a optype= '. $type. ' pid= ' Showpage_ '. $i. ' href= ' javascript:void (0) "'. $strClass. ' > '. $i. ' </a> ';
}
}
Else
{
$intOffset = Floor ($intSize/2);
$strPage. = $link!= '? ' <a href= "'. Preg_replace ("/-p (\d+)/"," P1 ", $link)." >1</a> ': ' <a optype= '. $type. ' "pid=" Showpage_1 "href=" javascript:void (0) ">1</a>";
if (($intPage-$intOffset) > 2)
{
$strPage. = ' <a class= ' Gpage_nobor ' >...</a> ';
}
for ($i = ($intPage-$intOffset) <=1?2: ($intPage-$intOffset); $i <= ($intPage + $intOffset) >= $intTotal? ( $intTotal-1):($intPage + $intOffset)); $i + +)
{
$strClass = $i = = $intPage? ' class= ' g_serpagcur ': ';
$strPage. = $link!= '? ' <a href= "'. Preg_replace ("/-p (\d+)/"," P ". $i, $link). ' '. $strClass. ' > ' $i. ' </a> ': ' <a optype= '. $type. ' pid= ' Showpage_ '. $i. ' href= ' javascript:void (0) "'. $strClass. ' > '. $i. ' </a> ';
}
if (($intPage-$intOffset) < ($intTotal-1))
{
$strPage. = ' <a class= ' Gpage_nobor ' >...</a> ';
}
$strPage. = $link!= '? ' <a href= "'. Preg_replace ("/-p (\d+)/"," P ". $intTotal, $link). ' > '. $intTotal. ' </a> ': ' <a optype= '. $type. ' pid= ' Showpage_ '. $intTotal. ' href= ' javascript:void (0) ' > '. $intTotal. ' </a> ';
}
}
if ($intPage < $intTotal)
{
$strPage. = $link!= '? ' <a href= "'. Preg_replace ("/-p (\d+)/"," P ". ( $intPage + 1), $link). ' " > Next >></a> ': ' <a optype= '. $type. ' pid= ' Showpage_ '. ($intPage + 1). ' " href= "javascript:void (0)" > next page >></a> ';
}
Else
{
$strPage. = ' <a href= ' javascript:void (0) ' > Next >></a> ';
}
}
$strPage. = "</div>";
return $strPage;
}

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.