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, see
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, see
The example in this article describes how to implement ThinkPHP paging and shares it with you for your reference. 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; // to maintain a set of confluence operations, copy a model in a shortest.
$ 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, it is the paging style. This is a bit messy, because the background framework has been downloaded online, and the style has not been prepared yet. This style can also be implemented by itself, which is 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.