搜索页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="jq.js"></script>
</head>
<body>
<form action="?r=sp/search" method="post">
<table border="1" align="center">
<tr>
<td><input type="text" class="price" name="price"></td>
<td><input type="submit" value="搜索" class="search"></td>
</tr>
</table>
</form>
</body>
</html>
控制器
class SpController extends Controller{
public $enableCsrfValidation = false;
public function actionLogin()
{
return $this->renderPartial('login');
}
public function actionSearch()
{
if(\Yii::$app->request->isPost)
{
$price=\yii::$app->request->post('price');
}
else
{
$price=\yii::$app->request->get('price');
}
$db=new Page_search;
$ac=$db->find()->where("username like '%$price%'")->count();
//接受分页的值
$page=\yii::$app->request->get('page');
//判断分页是否存在
$pages=isset($page)?$page:1;
//设置每页显示条数
$number=2;
//设置总页数
$num_page=ceil($ac/$number);
//设置上下页
$last=$pages-1<=1?$pages:$pages-1;
$next=$pages+1>=$num_page?$num_page:$pages+1;
//计算偏移量
$offset=($pages-1)*$number;
$where="username like '%$price%'";
$sql="select * from page_search where $where limit $offset,$number";
$list=$db->findBySql($sql)->asArray()->all();
$p['last']=$last;
$p['next']=$next;
$p['num_page']=$num_page;
$p['price']=$price;
return $this->renderPartial('search',['list'=>$list,'page'=>$p]);
}
}
搜索后展示页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table border="center">
<tr>
<td>姓名</td>
<td>电话</td>
<td>地址</td>
</tr>
<?php foreach ($list as $key => $v): ?>
<tr>
<td><?= $v['username']?></td>
<td><?= $v['userphone']?></td>
<td><?= $v['usersite']?></td>
</tr>
<?php endforeach ?>
<tr>
<td><a href="?r=sp/search&page=1&price=<?= $page['price']?>">首页</a></td>
<td><a href="?r=sp/search&page=<?= $page['last']?>&price=<?= $page['price']?>">上页</a></td>
<td><a href="?r=sp/search&page=<?= $page['next']?>&price=<?= $page['price']?>">下页</a></td>
<td><a href="?r=sp/search&page=<?= $page['num_page']?>&price=<?= $page['price']?>">尾页</a></td>
</tr>
</table>
</body>
</html>