//控制器
$username = Yii::$app->request->post('username'); //接收搜索的数
$where = 1;
if(!empty($username)){
$where = " username like '%$username%'";
}
$page = Yii::$app->request->post('pages');
//判断当前页码是否存在
$pages = isset($page) ? $page : 1 ;
//计算总条数
$count = Modi::find()->count();
//设置每一页显示的条数
$pageSize = 3 ;
//计算总页数
$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);' 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>";
//查询分页后的数据信息
$sql = "select * from modi where $where limit $offset,$pageSize";
$userInfo = Modi::findBySql($sql)->asArray()->all();
//print_r($userInfo);die;
//搜索后关键字标红
foreach($userInfo as $key => $value){
$userInfo[$key]['username'] = str_replace($username,"<font color='red'>$username</font>",$value['username']);
}
return $this->renderPartial('show',['userInfo'=>$userInfo,'page'=>$str]);
//展示页面
<?php echo $page;?>