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

之前每次遇到分页,总是得自己写,觉得挺繁琐的,所以本着通用的原则,写了一个分页方法,特此记录。 目前此分页支持静态化地址分页和无链接地址时的ajax分页(但是js得自己写): 支持的静态地址如下:www.example.com/xxx-xxx-p1-xxxx-xx.html 其他形式静态化需根据自己情况进行改写 支持ajax方式分页时,$link参数为空,但提供了pid和optype,其中pid用于获取页码页数,optype用于一个页面存在多个分页时区分当前触发动作属于哪个分页逻辑
<div class="codetitle"><a style="CURSOR: pointer" data="64796" class="copybut" id="copybut64796" onclick="doCopy('code64796')"> 代码如下:

<div class="codebody" id="code64796">
/**

获取页码
*
**

@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="'.pregreplace("/-p(\d+)/","p".($intPage-1),$link).'"><<上一页':'<a optype="'.$type.'" pid="showpage'.($intPage-1).'" href="javascript:void(0)"><<上一页';
else
$strPage .= '<a href="javascript:void(0)"><<上一页';
//窗口宽度大于等于总页数
if( ($intSize+2) >= $intTotal )
{
for($i=1;$i<=$intTotal;$i++)
{
$strClass = $i == $intPage ? 'class="g_serpagcur"' : "";
$strPage .= $link!=''?'<a href="'.pregreplace("/-p(\d+)/","p".$i,$link).'" '.$strClass.'>'.$i.'':'<a optype="'.$type.'" pid="showpage'.$i.'" href="javascript:void(0)" '.$strClass.'>'.$i.'';
}
}
else
{
if($intPage < ceil($intSize/2))
{
for($i=1;$i<=$intSize;$i++)
{
$strClass = $i == $intPage ? 'class="g_serpagcur"' : "";
$strPage .= $link!=''?'<a href="'.pregreplace("/-p(\d+)/",$link).'" '.$strClass.'>'.$i.'':'<a optype="'.$type.'" pid="showpage'.$i.'" href="javascript:void(0)" '.$strClass.'>'.$i.'';
}
$strPage .= $link!=''?'<a href="'.preg_replace("/-p(\d+)/","p".$intTotal,$link).'" >'.$intTotal.'':''.$intTotal.'';
}
elseif(($intTotal-$intPage) < ceil($intSize/2))
{
$strPage .= $link!=''?'<a href="'.preg_replace("/-p(\d+)/","p1",$link).'">1':'<a optype="'.$type.'" pid="showpage_1" href="javascript:void(0)">1';
for($i = ($intTotal + 1 - $intSize);$i++;$i<=$intTotal)
{
$strClass = $i == $intPage ? 'class="g_serpagcur"' : "";
$strPage .= $link!=''?'<a href="'.pregreplace("/-p(\d+)/",$link).'" '.$strClass.'>'.$i.'':'<a optype="'.$type.'" pid="showpage'.$i.'" href="javascript:void(0)" '.$strClass.'>'.$i.'';
}
}
else
{
$intOffset = floor($intSize/2);
$strPage .= $link!=''?'<a href="'.preg_replace("/-p(\d+)/",$link).'">1':'<a optype="'.$type.'" pid="showpage_1" href="javascript:void(0)">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!=''?'<a href="'.pregreplace("/-p(\d+)/",$link).'" '.$strClass.'>'.$i.'':'<a optype="'.$type.'" pid="showpage'.$i.'" href="javascript:void(0)" '.$strClass.'>'.$i.'';
}
if( ($intPage - $intOffset) < ($intTotal - 1))
{
$strPage .= '';
}
$strPage .= $link!=''?'<a href="'.pregreplace("/-p(\d+)/",$link).'">'.$intTotal.'':'<a optype="'.$type.'" pid="showpage'.$intTotal.'" href="javascript:void(0)">'.$intTotal.'';
}
}
if($intPage < $intTotal)
{
$strPage .= $link!=''?'<a href="'.pregreplace("/-p(\d+)/","p".($intPage+1),$link).'">下一页>>':'<a optype="'.$type.'" pid="showpage'.($intPage+1).'" href="javascript:void(0)">下一页>>';
}
else
{
$strPage .= '<a href="javascript:void(0)">下一页>>';
}
}
$strPage .= "
";
return $strPage;
}

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...