//控制器方法
public function actionStrylist(){
$username = \Yii::$app->request->post('username'); //接收搜索的数
$where=1;
if(!empty($username)){
$where = " b_name like '%$username%'";
}
$page = \Yii::$app->request->post('pages');
//判断当前页码是否存在
$pages = isset($page) ? $page : 1 ;
//计算总条数
$count = Brand::find()->count();
//设置每一页显示的条数
$pageSize = 5 ;
//计算总页数
$pageSum = ceil($count/$pageSize);
//计算偏移量
$offset = ($pages - 1)*$pageSize;
//计算上一页 下一页
$last = $pages<=1 ? 1 : $pages-1 ;
$next = $pages>=$pageSum ? $pageSum : $pages+1 ;
//拼接A链接
$str = '';
$str .= "<a href='javascript:void(0);' >共"."$count"."项目 ".$pageSum."页</a> ";
$str .= "<a href='javascript:void(0);' onclick='page(1)'>首页</a> ";
$str .= "<a href='javascript:void(0);' onclick='page($last)'>上一页</a> ";
$str .= "<a href='javascript:void(0);' onclick='page($next)'>下一页</a> ";
$str .= "<a href='javascript:void(0);' onclick='page($pageSum)'>尾页</a> ";
$str .= "<a href='javascript:void(0);' >第"."$pages"."页</a> ";
//查询分页后的数据信息
$sql = "SELECT a.b_id,a.b_name,a.b_company,a.b_addtime,b.c_name,c.a_area,d.d_name,e.shop_name,f.t_price FROM brand a,cate b,area c,dustry d,shop_type e,tment_scope f where a.c_id=b.c_id and a.r_id=c.a_id and a.i_id=d.d_id and a.t_id=e.s_id and a.p_id=f.t_id and $where limit $offset,$pageSize";
$userInfo = Brand::findBySql($sql)->asArray()->all();
//搜索后关键字标红
foreach($userInfo as $key => $value){
$userInfo[$key]['b_name'] = str_replace($username,"<font color='red'>$username</font>",$value['b_name']);
}
return $this->render('show',['arr'=>$userInfo,'page'=>$str,'username'=>$username]);
}
show视图页
<div class="public-content-header">
<input type="text" class="page-input" id="keyword" value="<?php echo $username?>">
<button onclick="page()">搜索</button>
</div>
echo分页变量
<div class="page">
<?php echo $page;?>
</div>
script 搜索分页方法 //加载jQuery
function page(page){
//获取搜索的数据
var username = $("#keyword").val();
//发送AJAX请求
$.ajax({
url:"index.php?r=industry/strylist",
data:{pages:page,username:username},
type:"POST",
success:function(msg){
$("#body").html(msg);
}
});
}