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

yipeiwu_com6年前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;
}

相关文章

php HtmlReplace输入过滤安全函数

复制代码 代码如下: // $rptype = 0 表示仅替换 html标记 // $rptype = 1 表示替换 html标记同时去除连续空白字符 // $rptype = 2 表示...

php实现通用的信用卡验证类

本文实例讲述了php实现通用的信用卡验证类。分享给大家供大家参考。 原文说明如下: Credit Card Validation Solution (PHP Edition) Vers...

PHP 数组学习排序全接触第1/2页

array_values($arr) array 返回数组所有元素 复制代码 代码如下: <?php $arr = array("a", "b", "c"); $arr = arr...

计算一段日期内的周末天数的php代码(星期六,星期日总和)

复制代码 代码如下: /* | Author: Yang Yu <niceses@gmail.com> | @param char|int $start_date 一个有效的...

php文件操作之文件写入字符串、数组的方法分析

php文件操作之文件写入字符串、数组的方法分析

本文实例讲述了php文件操作之文件写入字符串、数组的方法。分享给大家供大家参考,具体如下: 场景一:用文本文档记录一些操作日志,因为对于一些频繁的操作,操作记录的数据量势必会很大,...