简单的php多页码分页

发布时间:2020-07-23 17:09:32 作者:ivyandrich
来源:网络 阅读:516

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


    目前此分页支持静态化地址分页和无链接地址时的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).'">&lt;&lt;上一页</a>':'<a optype="'.$type.'"  pid="showpage_'.($intPage-1).'" href="javascript:void(0)">&lt;&lt;上一页</a>';
        else
            $strPage .= '<a href="javascript:void(0)">&lt;&lt;上一页</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).'">下一页&gt;&gt;</a>':'<a optype="'.$type.'"  pid="showpage_'.($intPage+1).'" href="javascript:void(0)">下一页&gt;&gt;</a>';
        }
        else
        {
            $strPage .= '<a href="javascript:void(0)">下一页&gt;&gt;</a>';
        }
    }
    $strPage .= "</div>";
    return $strPage;
}


推荐阅读:
  1. PHP之简单的分页类
  2. php 分页 分页类 简单实用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php web 多页码分页

上一篇:php怎么实现sql防注入

下一篇:MySQL联表查询中left-join常见问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》