search
Homephp教程PHP源码Thinkphp实现分页

Thinkphp实现分页

May 26, 2016 am 08:20 AM

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;//返回给调用处。。赋值。

前端展示页

<!-- 组件 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>

调用

$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()
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),