php 分页函数

作者: hopevow | 来源:发表于2016-11-12 11:00 被阅读38次

    在一个网站中,分布必不可少,如何巧妙的组织一个可控性强的分页函数显得犹为重要,前面在模型类中已经把它交给了函数multipage,这里,对它进行完善

    function multipage($page, $page_num, $url, $isajax = 0) {
        
        $count = count($url);
        if ($isajax) {
            $css = " class='__ajax' action=\\".__page-box\\" ";
        } else {
            $css = '';
        }
        //html开始
        $html = '<ul class="am-pagination pagination">';
    
        //构造上一页
        if ($page == 1) {
            $html .= "<li class='am-active active'><a href='javascript:;>< </a></li>";
        }else {
            $pre = $page - 1;
            $url[$count] = $pre;
            $html .= "<li><a $css title=\\"上一页\\" href='" . implode('.', $url) . "'><</a></li>";
        }
        //构造第一页
        if ($page == 1) {
            $html .= "<li class='am-active active'><a href='javascript:;'>1</a></li>";
        } else {
            $url[$count] = 1;
            $html .= "<li><a $css href='" . implode('.', $url) . "'>1</a></li>";
        }
        
        //构造显示中间的页码
    
        if (1) {
            $arr = range($page-1, $page + 2, 1);
        } else {
            $arr = range(2, 3, 1);
            $tmp1 = array_merge(range($page - 2, $page + 2, 1) , range($page_num - 2, $page_num + 1, 1));
            $arr = array_unique(array_merge($arr, $tmpl));
        }
        
    
        $j = 1;
        foreach ($arr as $i) {
            if ($page_num < $i || $i < 2) {
                continue;
            }
    
            if ($j > 0 and $i - $j > 1) {
                $html .= "<li><a class='nolink'>...</a></li>";
            }
    
            if ($page == $i) {
                $html .= "<li class='am-active active'><a href='javascript:;'>$i</a></li>";
            } else {
                $url[$count] = $i;
                $html .= "<li><a $css href='" . implode('.', $url) . "'>$i</a></li>";
            }
            $j = $i;
        }
    
        if ($page_num - $i > 1) {
            $html .= "<li><a class='nolink'>...</a></li>";
            $url[$count] = $page_num;
            $html .= "<li><a href='" . implode('.', $url) . "'>$page_num</a></li>";
        } elseif ($page_num - $i == 1) {
            $url[$count] = $page_num;
            $html .= "<li><a $css href='" . implode('.', $url) . "'>$page_num</a></li>";
        }
    
        if ($page == $page_num) {
            $html .= "<li class='am-active active'><a href='javascript:;'>></a></li>";
        } else {
            $next = $page + 1;
            $url[$count] = $next;
            $html .= "<li><a $css href='" . implode('.', $url) . "' title='下一页'>></a></li>";
        }
        $html .= "</ul>";
    
        echo $html;
    
    }
    
    

    相关文章

      网友评论

        本文标题:php 分页函数

        本文链接:https://www.haomeiwen.com/subject/qmsfpttx.html