Home > php教程 > PHP源码 > Thinkphp实现分页

Thinkphp实现分页

PHP中文网
Release: 2016-05-26 08:20:31
Original
1079 people have browsed it

php代码

//生成分页的方法
//obj是需要分页表的实例化对象,limit是当前页左右显示的条数。
//$map为分页时where 的条件。
//$this->size是我自己类下的变量。为每页显示的条数
    public function CreatePage($obj,$limit,$map=''){
        //总条数
        $totalRows=$obj->where($map)->count();
        //总页数
        $totalPage=(round($totalRows/$limit,0));
        //当前页。 
        $nowPage=I('get.p')?I('get.p'):1;
        if($nowPage>$this->size){
            $start=$nowPage-$this->size;
        }
        else{
            $start=1;
        }
        if($totalPage>($nowPage+$this->size)){
            $end=$nowPage+$this->size;
        }
        else{
            $end=$totalPage;
        }
        if($nowPage==$totalPage){
            $start=$nowPage-$this->size;$end=$totalPage;
        }
        $page['totalRows']=$totalRows;
        $page['totalPage']=$totalPage;
        $page['nowPage']=$nowPage;
        $page['start']=$start;
        $page['end']=$end;
        return $page;//返回给调用处。。赋值。
Copy after login

前端展示页

<!-- 组件 pagechangesss -->
<if condition="$page[&#39;totalPage&#39;] gt 1">
<div class="pagechange">
    <if condition="$page[&#39;nowPage&#39;] gt 1"><a href="{:Q(&#39;p&#39;,($page[&#39;nowPage&#39;]-1))}">上一页</a></if>
    <for start="$page[&#39;start&#39;]" end="$page[&#39;end&#39;]+1">
        <if condition="$i eq $page[&#39;nowPage&#39;]">
            <span class="current">{$i}</span>
            <else/>
            <a href="{:Q(&#39;p&#39;,$i)}">{$i}</a>
        </if>
    </for>
    <if condition="$page[&#39;nowPage&#39;] lt $page[&#39;totalPage&#39;]"><a href="{:Q(&#39;p&#39;,($page[&#39;nowPage&#39;]+1))}">下一页</a></if>
    <input type="text" id="jumpPage" />
    <script>
        var url="{:Q(&#39;p&#39;,($page[&#39;nowPage&#39;]+1))}";
        var nowpage="{$page[&#39;nowPage&#39;]}";
        var totalPage="{$page[&#39;totalPage&#39;]}";
    </script>
    <a href="javascript:void(0)" onclick="page(url,totalPage,nowpage)">跳转</a>
</div>
</if>
Copy after login

调用

$page=$this->CreatePage($this->obj,$this->size,$map);
//此处为TP的查询操作。
$data=$this->obj
    ->where($map)
    ->field(&#39;del,sequence,channel&#39;,true)
    //重点是这里。$page[&#39;nowPage&#39;]页码。
    //$this->size 显示的条数
    ->page($page[&#39;nowPage&#39;],$this->size)
    ->order(&#39;sequence&#39;)
    ->select()
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template