搜索后分页

搜索页面

<!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>



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值