Home  >  Article  >  Backend Development  >  PHP page number form paging function supports static address and ajax paging_PHP tutorial

PHP page number form paging function supports static address and ajax paging_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:03890browse

Every time I encountered pagination before, I always had to write it myself, which seemed quite tedious, so based on the general principle, I wrote a pagination method and recorded it here.

Currently, this paging supports static address paging and ajax paging when there is no link address (but js must be written by yourself):

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

Other forms of staticization need to be rewritten according to your own situation

When supporting ajax paging, the $link parameter is empty, but pid and optype are provided. Among them, pid is used to obtain the page number, and optype is used to distinguish which paging logic the current trigger action belongs to when there are multiple pages on a page

Copy code The code is as follows :

/***************************************************** ******
*
* Get page number
*
****************************** *****************************
*
* @params string $link link address (the link is empty You can use ajax to turn pages)
*
* @params int $intPage Current page number
*
* @params int $intTotal Total number of pages
*
* @params int $intSize Number of pages to be displayed
*
* @params string $type link type (multiple pages are used to distinguish page turning areas)
*
******* *************************************************** *
*
* @return array
*/
private function formatPage($link="",$intPage,$intTotal,$intSize=3,$type="")
{
$strPage = '
';
if($intTotal > 0)
{
if($intPage > 1)
$strPage .= $link!=''?'<<上一页':'<<上一页';
else
$strPage .= '<<上一页';
//窗口宽度大于等于总页数
if( ($intSize+2) >= $intTotal )
{
for($i=1;$i<=$intTotal;$i++)
{
$strClass = $i == $intPage ? 'class="g_serpagcur"' : "";
$strPage .= $link!=''?''.$i.'':''.$i.'';
}
}
else
{
if($intPage < ceil($intSize/2))
{
for($i=1;$i<=$intSize;$i++)
{
$strClass = $i == $intPage ? 'class="g_serpagcur"' : "";
$strPage .= $link!=''?''.$i.'':''.$i.'';
}
$strPage .= $link!=''?''.$intTotal.'':''.$intTotal.'';
}
elseif(($intTotal-$intPage) < ceil($intSize/2))
{
$strPage .= $link!=''?'1':'1';
for($i = ($intTotal + 1 - $intSize);$i++;$i<=$intTotal)
{
$strClass = $i == $intPage ? 'class="g_serpagcur"' : "";
$strPage .= $link!=''?''.$i.'':''.$i.'';
}
}
else
{
$intOffset = floor($intSize/2);
$strPage .= $link!=''?'1':'1';
if( ($intPage - $intOffset) > 2)
{
$strPage .= '';
}
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!=''?''.$i.'':''.$i.'';
}
if( ($intPage - $intOffset) < ($intTotal - 1))
{
$strPage .= '';
}
$strPage .= $link!=''?''.$intTotal.'':''.$intTotal.'';
}
}
if($intPage < $intTotal)
{
$strPage .= $link!=''?'下一页>>':'下一页>>';
}
else
{
$strPage .= 'next page>>';
}
}
$strPage .= "
";
return $strPage;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/746870.htmlTechArticleEvery time I encountered pagination before, I always had to write it myself, which felt quite cumbersome, so I followed the general principle , wrote a paging method, which is hereby recorded. Currently this paging supports static address segmentation...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn