yii框架中,搜尋的表單和後臺執行,以及分頁
阿新 • • 發佈:2019-01-10
/**
* 管理員搜尋檢視那些課程
* 分頁顯示
* 多條件搜尋
*
*/
public function actionSearch(){
//接搜尋傳過來的值
$data=yii::$app->request->get();
//例項化查詢類
$query=new \yii\db\Query();
//查詢表
$query->from('classes');
//判斷班級名稱的值是否存在,不存在則為空
if(isset($data['cla_name'])&&$data['cla_name']!=''){
$query->andWhere(['like','cla_name',$data['cla_name']]); //是andWhere 不是addwhere 括號裡是陣列
}else{
$data['cla_name']='';
}
//判斷班級描述是否為空,否則的話就為空
if(isset($data['cla_name'])&&$data['cla_desc']!=''){
$query->andWhere(['like','cla_desc',$data['cla_desc']]);
}else{
$data['cla_desc']='';
}
//判斷新增的時間 (特殊情況,在此不在進行判斷,直接賦值是空)
$data['cla_time']='';
// $cla=new Classes();
// $arr=$cla->find();
$pages= new Pagination([
'totalCount'=>$query->count(),
'pageSize' => 2 //每頁顯示條數
]);
$models = $query->offset($pages->offset) //查詢出來的每一頁的
->limit($pages->limit)
->all();
return $this->render('search', [
'models' => $models,
'pages' => $pages,
'data' =>$data, //搜尋的預設值
]);
}
* 管理員搜尋檢視那些課程
* 分頁顯示
* 多條件搜尋
*
*/
public function actionSearch(){
//接搜尋傳過來的值
$data=yii::$app->request->get();
//例項化查詢類
$query=new \yii\db\Query();
//查詢表
$query->from('classes');
//判斷班級名稱的值是否存在,不存在則為空
if(isset($data['cla_name'])&&$data['cla_name']!=''){
$query->andWhere(['like','cla_name',$data['cla_name']]); //是andWhere 不是addwhere 括號裡是陣列
}else{
$data['cla_name']='';
}
//判斷班級描述是否為空,否則的話就為空
if(isset($data['cla_name'])&&$data['cla_desc']!=''){
$query->andWhere(['like','cla_desc',$data['cla_desc']]);
}else{
$data['cla_desc']='';
}
//判斷新增的時間 (特殊情況,在此不在進行判斷,直接賦值是空)
$data['cla_time']='';
// $cla=new Classes();
// $arr=$cla->find();
$pages= new Pagination([
'totalCount'=>$query->count(),
'pageSize' => 2 //每頁顯示條數
]);
$models = $query->offset($pages->offset) //查詢出來的每一頁的
->limit($pages->limit)
->all();
return $this->render('search', [
'models' => $models,
'pages' => $pages,
'data' =>$data, //搜尋的預設值
]);
}