TP搜索,分页(保留搜索条件)
Controller层/控制器层/VoController.class.php
namespace Home\Controller;
use Think\Controller;
class VoController extends Controller {
public function index(){
if(IS_GET){
$this->display();
}
if(IS_POST){
$data = array(
'name' => I('username'),
'sort' => I('sort'),
);
$res = M('Vo')->add($data);
if($res){
$this->success("提交成功",U('Vo/show'));
}else{
$this->error("提交失败");
}
}
}
public function show(){
$where = "1=1";
if(!empty($username = I("username"))){
$where .= " and username = '$username'";
}
if(!empty($sort = I("sort"))){
$where .= " and sort = '$sort'";
}
$Vo = M('Vo');
$count = $Vo->where($where)->count();
$Page = new \Think\Page($count,2);
$show = $Page->show();
$list = $Vo->where($where)->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('list',$list);
$this->assign('username',$username);
$this->assign('sort',$sort);
$this->assign('page',$show);
$this->display();
}
}
View层/视图层/index.html
添加index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="{:U('Home/Vo/index')}" method="post">
<table>
<tr>
<td>学生姓名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>学生学号</td>
<td><input type="text" name="sort"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
View层/视图层/show.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script style="__PUBLIC__/Home/js/jquery.js"></script>
<style>
td{width: 180px;text-align: center}
.num, .current, .prev{margin-left: 15px;}
</style>
<body>
<center>
<form action="{:U('Home/Vo/show')}" method="get">
姓名:<input type="text" name="username" value="{$username}">
学号:<input type="text" name="sort" value="{$sort}">
<input type="submit" value="搜索">
</form>
<table>
<tr>
<td>id</td>
<td>姓名</td>
<td>学号</td>
<td>操作</td>
</tr>
<foreach name="list" item="vo">
<tr>
<td>{$vo.id}</td>
<td>{$vo.username}</td>
<td>{$vo.sort}</td>
<td><a href="">删除</a> | <a href="">修改</a></td>
</tr>
</foreach>
</table>
{$page}
</center>
</body>
</html>