一聚教程网:一个值得你收藏的教程网站

热门教程

php+smarty模板分页程序代码

时间:2022-06-25 02:18:51 编辑:袖梨 来源:一聚教程网

功能比较全的小分页,传几个参数上去就行,计算后输出数组,没什么新意,无非增加了个缩短页码,结果都给压到一个数组,方便输出和调用..附一个smarty的输出样例….

 代码如下 复制代码

/**
 * 分页,公共方法
 * @param int $page     当前页
 * @param int $size     数据条数
 * @param int $count    总记录数
 * @return mixed 页码数组 ('当前页',上一页','下一页','首页','末页','页码') ('current','prev','next','first','last','pages'=>array(0=>array('name'=>1,'page'=>'1')))
 */
public function pager($page,$size,$count){
 //省略页码标示
 $name_prev_more = '...';
 $name_next_more = '...';
 //显示页码数量
 $pages_length   = 9;

    //计算页面总数
    if($count){
     $num = ceil($count/$size);
    }

    if($num && $page){
        //当前页
        $result['current']= $page;

        //上一页
        if($page - 1){
            $result['prev'] = $page - 1;
        }else{
            $result['prev'] = 1;
        }

        //下一页
        if($num == $page){
            $result['next'] = $num;
        }else{
            $result['next'] = $page + 1;
        }

        $pages = array();

        //省略页码的长度计算
        if($num >= $pages_length){
            $length = intval($pages_length/2);
        }else{
         $length = intval($num/2);
        }

        //页码
        for ($i=1;$i<=$num;$i++){
            if($page == $i){
                $pages[$i]['active'] = 'active';
            }
            if($i >= $page-$length && $i <=$page+$length){
                $pages[$i]['page'] = $i;
                $pages[$i]['name'] = $i;
            }
        }

        //开头省略过长页码
        if($page - 1 >$length){
            $prev_more['page'] = $page - $length -1;
            $prev_more['name']  = $name_prev_more;
            array_unshift($pages,$prev_more);
        }

        //结尾省略过长页码
        if($num - $page >$length){
            $next_more['page'] = $page + $length + 1;
            $next_more['name']  = $name_next_more;
         array_push($pages,$next_more);
        }

        $result['pages'] = $pages;
        //首页,末页
        $result['first'] = 1;
        $result['last']  = $num;
        //print_r($result);   //TODO:查看数据结构
        return $result;
    }
    return '';
}

返回数据结构

pager(1,10,11)

Array
(
    [current] => 1
    [prev] => 1
    [next] => 2
    [pages] => Array
        (
            [1] => Array
                (
                    [active] => active
                    [page] => 1
                    [name] => 1
                )

            [2] => Array
                (
                    [page] => 2
                    [name] => 2
                )

        )

    [first] => 1
    [last] => 2
)

pager(1,1,11);

Array
(
    [current] => 1
    [prev] => 1
    [next] => 2
    [pages] => Array
        (
            [1] => Array
                (
                    [active] => active
                    [page] => 1
                    [name] => 1
                )

            [2] => Array
                (
                    [page] => 2
                    [name] => 2
                )

            [3] => Array
                (
                    [page] => 3
                    [name] => 3
                )

            [4] => Array
                (
                    [page] => 4
                    [name] => 4
                )

            [5] => Array
                (
                    [page] => 5
                    [name] => 5
                )

            [6] => Array
                (
                    [page] => 6
                    [name] => ...
                )

        )

    [first] => 1
    [last] => 11
)

php 调用及 smarty 输出

php调用(new smarty什么的就不写了)

 代码如下 复制代码
$pager = pager($page,$size,$count);
$smarty->assign('pager',$pager);

smarty 模板

 代码如下 复制代码


   
    {foreach from=$pager.pages item=item}
         {$item.name}
    {/foreach}
   

返回数据结构

 代码如下 复制代码

pager(6,1,11)

Array
(
    [current] => 6
    [prev] => 5
    [next] => 7
    [pages] => Array
        (
            [0] => Array
                (
                    [page] => 1
                    [name] => ...
                )

            [1] => Array
                (
                    [page] => 2
                    [name] => 2
                )

            [2] => Array
                (
                    [page] => 3
                    [name] => 3
                )

            [3] => Array
                (
                    [page] => 4
                    [name] => 4
                )

            [4] => Array
                (
                    [page] => 5
                    [name] => 5
                )

            [5] => Array
                (
                    [active] => active
                    [page] => 6
                    [name] => 6
                )

            [6] => Array
                (
                    [page] => 7
                    [name] => 7
                )

            [7] => Array
                (
                    [page] => 8
                    [name] => 8
                )

            [8] => Array
                (
                    [page] => 9
                    [name] => 9
                )

            [9] => Array
                (
                    [page] => 10
                    [name] => 10
                )

            [10] => Array
                (
                    [page] => 11
                    [name] => ...
                )

        )

    [first] => 1
    [last] => 11
)

热门栏目