php页码形式分页函数支持静态化地址及ajax分页

yipeiwu_com5年前PHP代码库
之前每次遇到分页,总是得自己写,觉得挺繁琐的,所以本着通用的原则,写了一个分页的方法,特此记录。

目前此分页支持静态化地址分页和无链接地址时的ajax分页(但是js得自己写):

支持的静态地址如下:www.example.com/xxx-xxx-p1-xxxx-xx.html

其他形式静态化需根据自己情况进行改写

支持ajax方式分页时,$link参数为空,但提供了pid和optype,其中pid用于获取该页码页数,optype用于一个页面存在多个分页时区分当前触发动作属于哪个分页逻辑
复制代码 代码如下:

/**********************************************************
*
* 获取页码
*
**********************************************************
*
* @params string $link 链接地址(链接为空时可以用ajax翻页)
*
* @params int $intPage 当前页数
*
* @params int $intTotal 总页数
*
* @params int $intSize 要显示的页数个数
*
* @params string $type 链接种类(多个翻页用于区分翻页区域)
*
**********************************************************
*
* @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).'"><<上一页</a>':'<a optype="'.$type.'" pid="showpage_'.($intPage-1).'" href="javascript:void(0)"><<上一页</a>';
else
$strPage .= '<a href="javascript:void(0)"><<上一页</a>';
//窗口宽度大于等于总页数
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(\d+)/","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).'">下一页>></a>':'<a optype="'.$type.'" pid="showpage_'.($intPage+1).'" href="javascript:void(0)">下一页>></a>';
}
else
{
$strPage .= '<a href="javascript:void(0)">下一页>></a>';
}
}
$strPage .= "</div>";
return $strPage;
}

相关文章

setcookie中Cannot modify header information-headers already sent by错误的解决方法详解

复制代码 代码如下:<?php   setcookie("username","bu",time()+3600);   echo "aaaa...

PHP实现的多维数组去重操作示例

本文实例讲述了PHP实现的多维数组去重操作。分享给大家供大家参考,具体如下: 最近用到了一个获取历史会议列表的问题,需要去这个多维数组去重后返回返回给客户端,这里使用了几个php的组装函...

PHP获取数组中重复最多的元素的实现方法

本文实例讲述了PHP获取数组中重复最多的元素的实现方法。分享给大家供大家参考。具体方法如下: 复制代码 代码如下:<?php  /**   ...

php HTML无刷新提交表单

通常对于无刷新提交表单,我们都是运用ajax实现的。前段时间跟着老大了解到另一种无刷新提交表单的方法,是利用iframe框架实现的。现在整理出来分享给大家。 第一种: html页面...

php $_SERVER当前完整url的写法

复制代码 代码如下:"http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];...