之前每次遇到分页,总是得自己写,觉得挺繁琐的,所以本着通用的原则,写了一个分页的方法,特此记录。 目前此分页支持静态化地址分页和无链接地址时的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; }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|