ThinkPHP paging instance _ php instance

Source: Internet
Author: User
This article mainly introduces ThinkPHP paging instances and describes the complete implementation steps of ThinkPHP paging, including the model layer, view layer, and style code, for more information about how to implement ThinkPHP paging, see the examples in this article. The specific method is as follows:

In the TP3.2 framework manual, there is a data paging, but too much code should be written every time, and there are some troubles in Chinese settings. as a program developer, it is necessary to sort it out:

Shows the instance running effect:

I. paging method:

The code is as follows:

/**
* The same code encapsulation of the TODO basic paging makes the front-end code less
* @ Param $ m model, reference transfer
* @ Param $ where query condition
* @ Param int $ pagesize number of entries per page
* @ Return \ Think \ Page
*/
Function getpage (& $ m, $ where, $ pagesize = 10 ){
$ M1 = clone $ m; // copy a model.
$ Count = $ m-> where ($ where)-> count (); // the join and other operations are reset after the connected operation.
$ M = $ m1; // copy a model to keep it in a fixed connection operation.
$ P = new Think \ Page ($ count, $ pagesize );
$ P-> lastSuffix = false;
$ P-> setConfig ('header ','

  • Total% TOTAL_ROW %Records per page% LIST_ROW %Entry% NOW_PAGE %Page/total% TOTAL_PAGE %Page
  • ');
    $ P-> setConfig ('prev', 'Previous page ');
    $ P-> setConfig ('next', 'next ');
    $ P-> setConfig ('last', 'Last page ');
    $ P-> setConfig ('first ', 'homepage ');
    $ P-> setConfig ('theme ',' % FIRST % UP_PAGE % LINK_PAGE % DOWN_PAGE % END % HEADER % ');

    $ P-> parameter = I ('Get .');

    $ M-> limit ($ p-> firstRow, $ p-> listRows );

    Return $ p;
    }

    The getpage method can be placed in the Application/Common/function of the TP framework. php. this document can be specifically used to place some common methods where they can be called (such as Controller files and View files ).

    II. call the paging method

    The code is as follows:

    $ M = M ('products ');
    $ P = getpage ($ m, $ where, 10 );
    $ List = $ m-> field (true)-> where ($ where)-> order ('Id desc')-> select ();
    $ This-> list = $ list;
    $ This-> page = $ p-> show ();
    View code


    {$ Page}


    3. Finally, the paging styleThis is a bit messy, because the background framework has been downloaded online, and the style has not been sorted out yet, this style can also be implemented by yourself, simple.

    The code is as follows:

    . Pagination ul {
    Display: inline-block;
    Margin-bottom: 0;
    Margin-left: 0;
    -Webkit-border-radius: 3px;
    -Moz-border-radius: 3px;
    Border-radius: 3px;
    -Webkit-box-shadow: 0 1px 2px rgba (0.05, 0 );
    -Moz-box-shadow: 0 1px 2px rgba (0.05, 0 );
    Box-shadow: 0 1px 2px rgba (0, 0, 0, 0.05 );
    }
    . Pagination ul li {
    Display: inline;
    }

    . Pagination ul li. rows {
    Line-height: 30px;
    Padding-left: 5px;
    }
    . Pagination ul li. rows B {color: # f00}

    . Pagination ul li a,. pagination ul li span {
    Float: left;
    Padding: 4px 12px;
    Line-height: 20px;
    Text-decoration: none;
    Background-color: # fff;
    Background: url ('../images/bottom_bg.png') 0px 0px;
    Border: 1px solid # d3dbde;
    /* Border-left-width: 0 ;*/
    Margin-left: 2px;
    Color: # 08c;
    }
    . Pagination ul li a: hover {
    Color: red;
    Background: # 0088cc;
    }
    . Pagination ul li. first-child a,. pagination ul li. first-child span {
    Border-left-width: 1px;
    -Webkit-border-bottom-left-radius: 3px;
    Border-bottom-left-radius: 3px;
    -Webkit-border-top-left-radius: 3px;
    Border-top-left-radius: 3px;
    -Moz-border-radius-bottomleft: 3px;
    -Moz-border-radius-topleft: 3px;
    }
    . Pagination ul. disabled span,. pagination ul. disabled a,. pagination ul. disabled a: hover {
    Color: #999;
    Cursor: default;
    Background-color: transparent;
    }
    . Pagination ul. active a,. pagination ul. active span {
    Color: #999;
    Cursor: default;
    }
    . Pagination ul li a: hover,. pagination ul. active a,. pagination ul. active span {
    Background-color: # f0c040;
    }
    . Pagination ul li. last-child a,. pagination ul li. last-child span {
    -Webkit-border-top-right-radius: 3px;
    Border-top-right-radius: 3px;
    -Webkit-border-bottom-right-radius: 3px;
    Border-bottom-right-radius: 3px;
    -Moz-border-radius-topright: 3px;
    -Moz-border-radius-bottomright: 3px;
    }

    . Pagination ul li. current a {color: # f00; font-weight: bold; background: # ddd}

    I hope this article will help you develop ThinkPHP framework programs.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.