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,
]);
}
VIEW
<form method="POST">
<!-- <input type="text" name="hospcode" placeholder="รหัสสถานบริการ" maxlength="5" size="15" > -->
<?php
$items = ArrayHelper::map(ChospitalAmp::find()->all(), 'hoscode', 'fullname');
echo Html::dropDownList('hospcode', $hospcode, $items, ['prompt' => '--- หน่วยบริการ ---']);
?>
<?php
$items = ArrayHelper::map(Csex::find()->all(), 'sex', 'sexname');
echo Html::dropDownList('sex', $sex, $items);
?>
เกิดระหว่าง:
<?php
echo yii\jui\DatePicker::widget([
'name' => 'date1',
'value' => $date1,
'language' => 'th',
'dateFormat' => 'yyyy-MM-dd',
'clientOptions' => [
'changeMonth' => true,
'changeYear' => true,
]
]);
?>
ถึง:
<?php
echo yii\jui\DatePicker::widget([
'name' => 'date2',
'value' => $date2,
'language' => 'th',
'dateFormat' => 'yyyy-MM-dd',
'clientOptions' => [
'changeMonth' => true,
'changeYear' => true,
]
]);
?>
<button class='btn btn-danger'> ตกลง </button>
</form>
**************
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use frontend\models\ChospitalAmp;
use frontend\models\Csex;
ไม่มีความคิดเห็น:
แสดงความคิดเห็น