The Page class in ThinkPHP can easily implement querying content pages. This article summarizes the usage of the Page class in the learning process. ThinkPHP
The Page class in ThinkPHP is in ThinkPHP/Extend/Library/ORG/Util/Page. class. php. Therefore, the Page class should be introduced before use:
The code is as follows:
Import ('org. Util. page'); // introduces the Page class.
$ Db = M ('ABC'); // instantiate the data table abc
$ Where = array (
'Id' => '2 ';
); // Condition statement $ where. The value of the field id in the example table is 2.
$ Count = $ db-> where ($ where)-> count (); // obtain the total number of qualified data count
$ Page = new Page ($ count, 10); // instantiate the page class. The total number of incoming data records and 10 entries per page are displayed.
$ Limit = $ page-> firstRow. ','. $ page-> listRows; // The number of data and content on each page $ limit
$ Result = $ db-> where ($ where)-> limit ($ limit)-> select (); // query results by page
$ This-> result = $ result; // value assignment
$ This-> show = $ page-> show (); // Obtain the bottom information of the page.
The above code is a basic statement for paging implementation. of course, friends who prefer native SQL statements can also use native SQL statements to query pages:
The code is as follows:
Import ('org. Util. page'); // introduces the Page class.
$ Db = M ('ABC'); // instantiate the data table abc
$ Where = array (
'Id' => '2 ';
); // Condition statement $ where. The value of the field id in the example table is 2.
$ Count = $ db-> where ($ where)-> count (); // obtain the total number of qualified data count
$ Page = new Page ($ count, 10); // instantiate the page class. The total number of incoming data records and 10 entries per page are displayed.
$ Modle = new Model (); // instance new data Model
$ SQL = 'SELECT id, name from abc where '. $ where. 'limit'. $ page-> firstRow.', '. $ page-> listRows; // SQL statement
$ Result = $ Modle-> query ($ SQL); // execute an SQL statement
$ This-> result = $ result
$ This-> show = $ page-> show ();
Of course, you can process the queried data before assigning values to the content obtained by the Distributed query. for example
The code is as follows:
...
$ Result = $ db-> where ($ where)-> limit ($ limit)-> select (); // query results by page
$ Res = abc ($ result); // The abc method (custom method or php function) sorts or restructured the result $ result.
$ This-> result = $ res; // value assignment