控制器:
<?php
namespace frontend\controllers;
use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\db\ActionIndex;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use frontend\models\Search;
use yii\data\Pagination;
/**
* Site controller
*/
class SearchController extends Controller
{
//展示数据
public function actionShow()
{
$where = Yii::$app->request->get();
$query = new \yii\db\Query();
$query->from('search');
//判断where条件
if(!empty($where['mold'])){
$query->andWhere(['mold'=>$where['mold']]);
}
if(!empty($where['grade'])){
$query->andWhere(['grade'=>$where['grade']]);
}
$data = $query->from('search')->all();
// return $this->render('show',['data'=>$data]);
$pages = new Pagination([
//'totalCount' => $countQuery->count(),
'totalCount' => $query->count(),
'pageSize' => '3' //每页显示条数
]);
$arr = $query->offset($pages->offset)->limit($pages->limit)->all();
return $this->render("show",['data'=>$data,'where'=>$where,'pages'=>$pages]);
}
}
视图层:
<?php
/* @var $this yii\web\View */
use yii\helpers\Html;
use yii\helpers\Url;
use yii\base\Widget;
use yii\widgets\ActiveForm;
use yii\widgets\LinkPager;
$this->title = '多条件查询';
?>
<h1><?= Html::encode($this->title) ?></h1>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
$form = ActiveForm::begin([
'action'=>Url::toRoute(['search/show']),
'method'=>'get',
]);
echo "公司类型:".Html::input('text','mold');
echo "公司等级:".Html::input('text','grade');
echo Html::submitButton('搜索',['class'=>'btn btn-primary']);
ActiveForm::end();
?>
<table class="table">
<tr>
<th>ID</th>
<th>公司名称</th>
<th>公司类型</th>
<th>金额</th>
<th>公司等级</th>
<th>还款方式</th>
<!-- <th>操作</th> -->
</tr>
<?php foreach ($data as $key => $value): ?>
<tr>
<td><?php echo $value['id']?></td>
<td><?php echo $value['company']?></td>
<td><?php echo $value['mold']?></td>
<td><?php echo $value['money']?></td>
<td><?php echo $value['grade']?></td>
<td><?php echo $value['mode']?></td>
<!-- <td><a href="?r=search/show&id=<?php //echo $value['id']?>">删除</a></td> -->
</tr>
<?php endforeach ?>
</table>
<?php
echo LinkPager::widget([
'pagination'=>$pages,
'nextPageLabel'=>"下一页",
'firstPageLabel'=>"首页",
])
?>
</body>
</html>
我试了一下分页,出现了问题。