Controller
public function actionIndex(){
$data = Yii::$app->request->post();
$hospcode = isset($data['hospcode']) ? $data['hospcode'] : 'null';
$sex = isset($data['sex']) ? $data['sex'] : '1,2';
$date1 =isset($data['date1']) ? $data['date1'] : '';
$date2 =isset($data['date2']) ? $data['date2'] : '';
$sql = "SELECT p.CID,p.`NAME`,p.LNAME,p.SEX,p.BIRTH
,TIMESTAMPDIFF(YEAR,p.BIRTH,CURDATE()) as AGE_Y
,TIMESTAMPDIFF(MONTH,p.BIRTH,CURDATE()) MOD 12 as AGE_M
,p.TYPEAREA,p.NATION,p.DISCHARGE from person p
WHERE p.DISCHARGE = 9 AND p.TYPEAREA in (1,3,5)
AND p.HOSPCODE = '$hospcode'
AND p.SEX in ($sex)";
if(!empty($date1) && !empty($date2)){
$sql.= " AND (p.BIRTH between '$date1' AND '$date2')";
}
$sql.= " ORDER BY p.BIRTH DESC,AGE_Y ASC,AGE_M ASC";
$rawData = \Yii::$app->db->createCommand($sql)->queryAll();
$person = new \yii\data\ArrayDataProvider([
//'key' => 'hoscode',
'allModels' => $rawData,
'pagination' => FALSE,
]);
return $this->render('index',[
'hospcode'=>$hospcode,
'person'=>$person,
'sql'=>$sql,
'sex'=>$sex,
'date1'=>$date1,
'date2'=>$date2,
]);
}