cotroller
public function actionIndex() {
$data = Yii::$app->request->post();
$school_id = isset($data['school_id']) ? $data['school_id'] : '';
$year = isset($data['year']) ? $data['year'] : '2558';
$school_term_id = isset($data['school_term_id']) ? $data['school_term_id'] : '1,2';
$sql = " SELECT s.school_name as school,
a.student_total, a.deciduous_caries,a.gingivitis,a.disorder,a.permanent_no,a.permanent_caries,
(SUM(a.deciduous_caries)*100)/SUM(a.exam) as resulta1,
(SUM(a.permanent_no)*100)/SUM(a.exam) as resulta2,
(SUM(a.permanent_caries)*100)/SUM(a.exam) as resulta3,
(SUM(a.gingivitis)*100)/SUM(a.exam) as resulta4,
(SUM(a.disorder)*100)/SUM(a.exam) as resulta5,
(SUM(a.exam)*100)/SUM(a.student_total) as resulta6,
SUM(a.exam)- SUM(a.deciduous_caries) as nodcaries,
SUM(a.exam)- SUM(a.permanent_caries) as nopcaries,
SUM(a.exam)- SUM(a.gingivitis) as nogin,
SUM(a.exam)- SUM(a.disorder) as nodis
FROM
(SELECT denta_id,deciduous_caries,gingivitis,disorder,permanent_no,permanent_caries,student_total,exam FROM denta_list ) a
JOIN denta a1 ON a1.denta_id=a.denta_id
JOIN school s ON s.school_id=a1.school_id
WHERE a1.year = '2558'
AND a1.school_id <> ''
AND a1.school_term_id IN (1)
GROUP BY a1.school_id ORDER BY s.school_type_id ASC ";
$rawdata = Yii::$app->db->createCommand($sql)->queryAll();
$dataProvider = new ArrayDataProvider([
'allModels' => $rawdata,
'pagination' => FALSE,
]);
return $this->render('index', [
'sql' => $sql,
'dataProvider' => $dataProvider,
'school_id' => $school_id,
'year' => $year,
'school_term_id' => $school_term_id,
]);
from
<form method = "post" >
<?php
$items = ArrayHelper::map(School::find()->all(), 'school_id', 'school_name');
echo Html::dropDownList('school_id', $school_id, $items, ['prompt' => '--- โรงเรียน ---']);
?>
<?php
$items = ArrayHelper::map(Year::find()->all(), 'year_id', 'year');
echo Html::dropDownList('year_id', $year, $items, ['prompt' => '--- ปีการศึกษา ---']);
?>
<?php
$items = ArrayHelper::map(Schoolterm::find()->all(), 'school_term_id', 'school_term_name');
echo Html::dropDownList('school_term_id', $school_term_id, $items, ['prompt' => '--- ภาคการศึกษา ---']);
?>
<button class='btn btn-danger'> ตกลง </button>
</form>
ลองวิธีนี้ดูนะครับ สร้าง model สมมติว่าเก็บไว้ใน common/models
<?php
namespace common\models;
class MySearchForm extends \yii\base\Model
{
public $school_id;
public $year;
public $school_term_id;
public function rules()
{
return [
[['school_id', 'year', 'school_term_id'], 'required']
];
}
public function attributeLabels()
{
return [
'school_id' => 'โรงเรียน',
'year' => 'ปีการศึกษา',
'school_term_id' => 'เทอม',
];
}
}
จากนั้นใน Controller ให้ทำการ
use common\models\MySearchForm;
public function actionIndex() {
$school_id = null;
$year = null;
$school_term_id = null;
$model = new MySearchForm();
if($model->load(Yii::$app->request->post()){
$school_id = !empty($model->school_id) ? $model->school_id : '';
$year = !empty($model->year) ? $model->year : '2558';
$school_term_id = !empty($model->school_term_id) ? $model->school_term_id : '1,2';
$sql = " SELECT s.school_name as school,
a.student_total,
a.deciduous_caries,
a.gingivitis,
a.disorder,
a.permanent_no,
a.permanent_caries,
(SUM(a.deciduous_caries)*100)/SUM(a.exam) as resulta1,
(SUM(a.permanent_no)*100)/SUM(a.exam) as resulta2,
(SUM(a.permanent_caries)*100)/SUM(a.exam) as resulta3,
(SUM(a.gingivitis)*100)/SUM(a.exam) as resulta4,
(SUM(a.disorder)*100)/SUM(a.exam) as resulta5,
(SUM(a.exam)*100)/SUM(a.student_total) as resulta6,
SUM(a.exam)- SUM(a.deciduous_caries) as nodcaries,
SUM(a.exam)- SUM(a.permanent_caries) as nopcaries,
SUM(a.exam)- SUM(a.gingivitis) as nogin,
SUM(a.exam)- SUM(a.disorder) as nodis
FROM (SELECT denta_id,deciduous_caries,gingivitis,disorder,permanent_no,permanent_caries,student_total,exam FROM denta_list ) a
JOIN denta a1 ON a1.denta_id=a.denta_id
JOIN school s ON s.school_id=a1.school_id
WHERE a1.year = '2558'
AND a1.school_id <> ''
AND a1.school_term_id IN (1)
GROUP BY a1.school_id
ORDER BY s.school_type_id ASC ";
$rawdata = Yii::$app->db->createCommand($sql)->queryAll();
$dataProvider = new ArrayDataProvider([
'allModels' => $rawdata,
'pagination' => FALSE,
]);
}//end if
return $this->render('index', [
'sql' => $sql,
'dataProvider' => $dataProvider,
'school_id' => $school_id,
'year' => $year,
'school_term_id' => $school_term_id,
'model' => $model
]);
}
สำหรับ View
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>
<?php $form = ActiveForm::begin()?>
<?php
$schools = ArrayHelper::map(School::find()->all(), 'school_id', 'school_name');
$years = ArrayHelper::map(Year::find()->all(), 'year_id', 'year');
$terms = ArrayHelper::map(Schoolterm::find()->all(), 'school_term_id', 'school_term_name');
?>
<?=$form->field($model, 'school_id')->dropDownList($schools, ['prompt' => '--- โรงเรียน ---'])?>
<?=$form->field($model, 'year')->dropDownList($years, ['prompt' => '--- ปีการศึกษา ---'])?>
<?=$form->field($model, 'school_term_id')->dropDownList($terms, ['prompt' => '--- ภาคการศึกษา ---'])?>
<?=Html::submitButton('ค้นหา', ['class' => 'btn btn-success'])?>
<?php ActiveForm::end()?>
ตอบ/อธิบาย