ThinkPHP paging class use detail

  • 2021-01-25 07:19:22
  • OfStack

1. The first step is to add paging to the MsgManage controller

Knowledge:
1. Trial of count function
2. Understand Page class instantiation operations and related parameters
3. The limit function is used
4. Understand ES11en functions

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

The code is as follows:


class MsgManageAction extends CommonAction {
    public function index(){
     import('ORG.Util.Page');
  //import Call is message/ThinkPHP Expansion packages under the framework directory Extend/Library/ORG/Util/ In the Page.class.php The class file 
     $count = M('board')->count();
  // call board Library, retrieve all data bar number 
     $page = new Page($count ,10);
  // instantiation Page Class, where the 1 Parameter is the total number of display bars, each fetch 10 Bar, which is the bottom $page->listRows The value of the 
  $limit = $page->firstRow . ',' . $page->listRows;
  //$page->firstRow Is the starting number of bars to look up. Default is 0 If the $page->listRows for 10 , then the first 2 page $page->firstRow for 10 "And so on 

  $board = M('board')->order('time DESC')->limit($limit)->select();
  // Note that this is an addition from the previous version ->limit($limit)
  $this->board = $board;
  $this->page = $page->show();
  // will $page->show() through show Method resolution $page The contents are displayed and assigned to template variables for template invocation 

  $this->display();
    }

 Public function delete(){
  $id = I('id','','intval');
  if(M('board')->delete($id)){
   $this->success(' Delete the success ',U('index'));
  }else{
   $this->error(' Delete failed ');
  }
 }
}

The show method is a new feature in version 3.1
ThinkPHP page in the output of the process is to read template files, then the template parsing support (also called the third party template parsing engine), but there are some 1 case, we did not define a template file, or put the template file stored in the database, and then the time for page output, we can't handle to read template files, version 3.1 for the situation to increase the content of parsing the output function.
The built-in templating engine has also been improved, and if the incoming template file does not exist, it is considered the incoming template parsing content. As a result, 3.1's View and Action classes have also been improved.
The ES32en method is used for template file rendering output, the ES33en method is used for template content rendering output, and the ES34en method still supports content parsing
Specific content can refer to :ThinkPHP3.1 new feature content parsing output
2. Add a paging module to the template file
Knowledge:
1, td cell merge
2, $page variable call display

Edit the file: admin Tpl/MsgManage/index html, add 1 paragraph tr used to display the page, the HTML 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> The publisher </th>
   <th> content </th>
   <th> Release time </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>

  // new tr Short code 
  <tr>
   <td colspan='5' align='center'>
   // will 5 The cells are merged and displayed in the center 
    {$page}
    // Display controller $this->page content 
   </td>
  </tr>
 </table>
</body>
</html>


Related articles: