您的位置:首页 > 编程语言 > PHP开发

yii 多条件查询,分页

2016-10-14 13:51 239 查看
视图层
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\LinkPager;
$form=ActiveForm::begin([
'action'=>Url::toRoute(['class/classshow']),//千万别忘记中括号,否则会找半天错
'method'=>'get',
]);
echo '班级名称:','',Html::input('text','uname',$data['uname']);
echo '简介关键字:',Html::input('text','keyword',$data['keyword']);
echo '添加时间:',Html::input('text','addtime',$data['addtime']);
echo Html::submitButton('查询');
ActiveForm::end();
?>
<table class="table">
<?php foreach($class as $key=>$val){ ?>
<tr>
<td><?=$val['c_id']?></td>
<td><?=$val['mingcheng']?></td>
<td><?=$val['intro']?></td>
<td><?=$val['add_time']?></td>
</tr>
<?php }?>
</table>
<?php echo LinkPager::widget([
'pagination' =>$pages]); ?>

model
<?php

namespace app\models;

use Yii;

class Class extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'classzk1';
}

/**
* @inheritdoc
*/
//    public function rules()
//    {
//        return [
//            [['content'], 'string'],
//            [['addtime', 'c_number'], 'integer'],
//            [['title'], 'string', 'max' => 100],
//            [['author'], 'string', 'max' => 50]
//        ];
//    }

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'c_id' => 'L_id',
'mingcheng' => 'Mingcheng',
'term' => 'Term',
'intro' => 'Intro',
'add_time' => 'Add_Time',

];
}
}

控制层
<?php
namespace backend\controllers;

use Yii;
//use yii\filters\AccessControl;
use yii\web\Controller;
use app\models\Login;
//use yii\filters\VerbFilter;
use yii\data\Pagination;
class ClassController extends Controller
{
//非法登陆
//定义构造方法, 传参id,$models=null
// public $layout=false;
public function __construct($id,$models=null){
parent::__construct($id,$models);
$session=yii::$app->session;
$sid=$session['username'];

if(!$sid){
//echo "非法登陆";die;
//return $this->redirect(array('/index/index/'));
// return $this->redirect('?r=login/index');
}
}
//查询班级信息
public function actionClassshow(){
//接收数据
$data=yii::$app->request<
de16
/span>->get();
// print_r($data);die;
//实例化查询类
$query=new \yii\db\Query();
//查询表
$query->from('classzk1');
//判断班级名称的值是否存在,并且值不为空
if(isset($data['uname'])&&$data['uname']!=''){
$query->andWhere(['like','mingcheng',$data['uname']]);
}else{
$data['uname']='';
}
//判断简介关键字的值是否存在,
if(isset($data['keyword'])&&$data['keyword']!=''){
$query->andWhere(['like','intro',$data['keyword']]);
}else{
$data['keyword']='';
}
//判断时间的值是否存在,
if(isset($data['addtime'])&&$data['addtime']!=''){
//具体日期为
$time=$data['addtime'].'-01-01';
//转化为时间戳
$data['time']=$time;
$query->andWhere(['<','add_time',$data['time']]);
}else{
$data['addtime']='';
}
//分页
//实例化框架自带分页类,获取总条数
$pages= new Pagination(['totalCount'=>$query->count()]);
//设置每页条数
$pages->setPageSize(4);

$class=$query->offset($pages->offset)->limit($pages->limit)->all();
//print_r($class);die;
return $this->render('classshow',['class'=>$class,'data'=>$data,'pages'=>$pages]);
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: