搜索 |
控制器:
//search是表单提交的input的name值
<?php
namespace Admin\Controller;
use Think\Controller;
header("Content-Type:text/html;charset=UTF-8");
class IndexController extends Controller {
public function index(){
$User = M('Emailuser'); // 实例化数据表对象
$where['accept']=1;
$where['id']=I('search')==""?array('like','%%'):array('lt',I('search'));
// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
$list = $User->where($where)->field('name,id,user,lxdh')->order('id')->page(I('p',0).','.C('PAGE_SIZE'))->select();
$this->assign('list',$list);// 赋值数据集
$count = $User->where($where)->count();// 查询满足要求的总记录数
$Page = new \Think\Page($count,C('PAGE_SIZE'));// 实例化分页类 传入总记录数和每页显示的记录数
$Page->lastSuffix=false;
$Page->setConfig('last','尾页');
$show = $Page->show();// 分页显示输出
$pageshow['count']=$count;
$pageshow['pagesize']=C('PAGE_SIZE');
$pageshow['page']=$show;
$this->assign('page',$pageshow);// 赋值分页输出
$this->display(); // 输出模板
}
}
模板页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="__PUBLIC__/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="table-responsive">
<table width="60%" class="table table-bordered table-hover table-striped">
<tr>
<th colspan="4">
<form class="form-inline" role="form" action="__SELF__" method="get">
<div class="form-group">
<label class="sr-only">Email</label>
<p class="form-control-static">请输入查询条件:</p>
</div>
<div class="form-group">
<label for="inputPassword2" class="sr-only">Password</label>
<input type="text" class="form-control input-sm" id="inputPassword2" name="search" placeholder="查询条件">
</div>
<button type="submit" class="btn btn-primary btn-sm">查询</button>
</form>
</th>
</tr>
<tr>
<th>id</th><th>姓名</th><th>账号</th><th>联系电话</th>
</tr>
<volist name="list" id="user_info">
<tr>
<td>{$user_info.id}</td>
<td>{$user_info.name}</td>
<td>{$user_info.user}</td>
<td>{$user_info.lxdh}</td>
</tr>
</volist>
<tr>
<td colspan="2" align="center">
<gt name="page.count" value="$page.pagesize">
{$page.page}
</gt>
</td>
<td colspan="2" align="center">
<if condition="$page.count gt C('PAGE_SIZE') ">
{$page.page}
<else />
</if>
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
基于bootstrap的样式改装的PAGE类: