ThinkPHP paging usage

Source: Internet
Author: User

1. First, add the paging method to the MsgManage controller.

Knowledge point:
1. Trial of the count function
2. Page class instantiation operations and related parameters
3. The limit function is used.
4. show Functions

Edit the admin/Lib/Action/MsgManageAction. class. php file.

The Code is as follows:
Copy codeThe Code is as follows:
Class MsgManageAction extends CommonAction {
Public function index (){
Import ('org. Util. page ');
// Import calls the Page. class. php class file in the Extend/Library/ORG/Util/extension package under the message/ThinkPHP framework directory.
$ Count = M ('board')-> count ();
// Call the board library to retrieve all data entries
$ Page = new Page ($ count, 10 );
// Instantiate the Page class. The first parameter is the total number of display items. Ten items are taken out each time, that is, the value of $ page-> listRows below.
$ Limit = $ page-> firstRow. ','. $ page-> listRows;
// $ Page-> firstRow indicates the start Number of the query. The default value is 0. If $ page-> listRows is 10, the value of $ page-> firstRow on page 1 is 10, and so on.

$ Board = M ('board')-> order ('time desc')-> limit ($ limit)-> select ();
// Note: here the limit ($ limit) is added from the previous version)
$ This-> board = $ board;
$ This-> page = $ page-> show ();
// Parse $ page-> show () through the show method. The $ page content is displayed and assigned to the template variable for template calling.

$ This-> display ();
}

Public function delete (){
$ Id = I ('id', '', 'intval ');

If (M ('board')-> delete ($ id )){
$ This-> success ('deleted successfully', U ('index '));
} Else {
$ This-> error ('deletion failed ');
}
}
}

The show method is a new feature available in version 3.1.
In ThinkPHP, the page output process is to read the template file and then parse the template (or call a third-party template engine for parsing). However, in some cases, we have not defined the template file, or save the template file in the database. When page output is performed at this time, we cannot read the template file, version 3.1 adds the content parsing output function in this case.
The built-in template engine is also improved. If the imported template file does not exist, it will be considered as the imported template parsing Content. Therefore, the View class and Action class of 3.1 have also been improved.
The display method is used to render the template file and the show method is used to render the template content. The show method still supports the content parsing function.
For details, refer to ThinkPHP3.1 new feature content parsing and output.
2. Add the paging module to the template file
Knowledge point:
1. Merge td Cells
2. $ page variable call display

Edit the file admin/Tpl/MsgManage/index.html and add tr to display paging information. The Code is as follows:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Message Board BackGround </title>
</Head>
<Body>
<Table class = "table" border = "1">
<Tr>
<Th> ID </th>
<Th> publisher </th>
<Th> content </th>
<Th> release date </th>
<Th> operation </th>
</Tr>

<Foreach name = 'board' item = 'B'>
<Tr>
<Td >{$ B. id} </td>
<Td >{$ B. username} </td>
<Td >{$ B. content} </td>
<Td> {$ B. time | date = 'Y-m-d H: I ', ###} </td>
<Td> <a href = "{: U ('admin. php/MsgManage/delete ', array ('id' => $ B ['id']), ''}"> delete </a> </td>
</Tr>
</Foreach>

// Add a short tr code
<Tr>
<Td colspan = '5' align = 'center'>
// Merge the five cells and display them in the center
{$ Page}
// Display the $ this-> page content in the Controller
</Td>
</Tr>
</Table>
</Body>
</Html>

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.