TP搜索,分页(保留搜索条件)简单实现

时间:2022-10-27 12:33:45

TP搜索,分页(保留搜索条件)

Controller层/控制器层/VoController.class.php

<?php
/**
* @author xiaozhang
*
* @email 915752851@qq.com
*
* @information Vo grade
*/

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'); // 实例化Vo对象
$count = $Vo->where($where)->count(); // 查询满足要求的总记录数
$Page = new \Think\Page($count,2); // 实例化分页类
// 传入总记录数和每页显示的记录数(25)
$show = $Page->show(); // 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
$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>

TP搜索,分页(保留搜索条件)简单实现
TP搜索,分页(保留搜索条件)简单实现