thinkphp3.2.3分页

时间:2021-06-11 15:41:32
View 
  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户信息输出</title>
<link href="__ROOT__/Public/Css/style.css" rel="stylesheet" type="text/css" />
<link href="__ROOT__/Public/Css/mypage.css" rel="stylesheet" type="text/css"/>
</head> <body>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
<tr>
<td colspan="3" bgcolor="#FFFFFF" class="title" align="center">当前登录用户:{$Think.session.admin}</td>
</tr>
<tr>
<td colspan="3" bgcolor="#FFFFFF" class="title" align="center">用户信息</td>
</tr>
<tr class="title">
<td bgcolor="#FFFFFF" width="44">ID</td>
<td bgcolor="#FFFFFF" width="120">用户名</td>
<td bgcolor="#FFFFFF" width="223">密码</td>
</tr>
<foreach name='select' item='user' >
<tr class="content">
<td bgcolor="#FFFFFF">&nbsp;{$user.id}</td>
<td bgcolor="#FFFFFF">&nbsp;{$user.word}</td>
<td bgcolor="#FFFFFF">&nbsp;{$user.pwd}</td>
</tr>
</foreach>
<tr class="content">
<!--<td colspan="3" bgcolor="#FFFFFF">&nbsp;{$page}</td>-->
<td colspan="3" bgcolor="#FFFFFF"><div class="pages">
{$page}
</div></td>
</tr>
</table>
</body>
</html>

Controller

  

  
 <?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$m = M('users');//连接表单
$count = $m->count();//查询数据数量
$p = getpage($count,1);//调用function.php下getpage函数
$list = $m->field(true)->order('id')->limit($p->firstRow, $p->listRows)->select();//分页
$this->assign('select', $list); // 赋值数据集
$this->assign('page', $p->show()); // 赋值分页输出
$this->display();
}
}

Application/Common/Common/function.php插入方法

  

 <?php
/**
* TODO 基础分页的相同代码封装,使前台的代码更少
* @param $count 要分页的总记录数
* @param int $pagesize 每页查询条数
* @return \Think\Page
*/
function getpage($count, $pagesize = 10) {
$p = new Think\Page($count, $pagesize);
$p->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
$p->setConfig('prev', '上一页');
$p->setConfig('next', '下一页');
$p->setConfig('last', '末页');
$p->setConfig('first', '首页');
//$p->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
$p->lastSuffix = false;//最后一页不显示为总页数
return $p;
}
?>