dewinataria
dewinataria

Reputation: 97

How to do query in Yii 2.0?

I want to make chart by using highcharts in Yii 2.0. I have made query to count how many person whose gender male and female. Just so you know, JenisKelamin is Gender, Laki-laki is male and Perempuan is female. And I have succeeded to make that query. The next task, I want to do query with "Usia". I want to know how many people who have usia < 25, usia between 25 up to 30, etc.

this is the table of Usia enter image description here

I have highchartscontroller and index view.

Highchartscontroller.php

<?php
namespace app\controllers;

use yii\web\Controller;
use app\models\Jeniskelaminreal;
use app\models\Jeniskelaminrealdoktor;
use app\models\Studi;
use yii\helpers\Json;

class HighchartsController extends Controller
{
    public function actionIndex()
    {

        $masuk= Jeniskelaminreal::find();
        $awal = $masuk->orderBy('TahunMasuk ASC')->one()->TahunMasuk;
        $akhir = $masuk->orderBy('TahunMasuk DESC')->one()->TahunMasuk;
        // $data = $masuk->all();
        $arr_l = [];
        $arr_p = [];
        $tahun = [];


        for($i=$awal;$i<=$akhir;$i++){

                if($awal == $i){
                    $jum_l = count($masuk->where(['TahunMasuk'=>$awal,'JenisKelamin'=>'Perempuan'])->all());
                    $jum_p = count($masuk->where(['TahunMasuk'=>$awal,'JenisKelamin'=>'Laki-laki'])->all());

                }elseif($i > $awal && $i <= $akhir){
                    $jum_l = count($masuk->where(['TahunMasuk'=>$i,'JenisKelamin'=>'Perempuan'])->all());
                    $jum_p = count($masuk->where(['TahunMasuk'=>$i,'JenisKelamin'=>'Laki-laki'])->all());
                }
                array_push($arr_l,$jum_l);
                array_push($arr_p,$jum_p);
                array_push($tahun,$i);
                }               


        $data['tahun'] = json_encode($tahun);
        $data['data_p'] = json_encode($arr_p);
        $data['data_l'] = json_encode($arr_l);

return $this->render('index',$data);
    }
}

index.php

<?php $this->registerJs("
$(function () {
    $('#my-chart').highcharts({
        title: {
            text: 'Jenis Kelamin',
            x: -20 //center
        },

        xAxis: {
            categories: $tahun
        },
        yAxis: {
            title: {
                text: 'Jumlah'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            valueSuffix: ''
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle',
            borderWidth: 0
        },
        series: [{
            name: 'Laki-laki',
            data: $data_l
        }, {
            name: 'Perempuan',
            data: $data_p
        }]
    });
});
")?>
</div>
</div>

Those codes above is to count how many people whose gender is male or female. But, I don't know how to make query to count how many person who has usia < 25, between 25-30, >30, etc. Could you please help me to solve that? Thank you in advance

Upvotes: 0

Views: 67

Answers (1)

Yasin Patel
Yasin Patel

Reputation: 5731

Query to find usia < 25

$masuk= Jeniskelaminreal::find()->where(['<', 'usia', 25]);

Query to find usia between 25 to 30

$masuk= Jeniskelaminreal::find()->where(['between', 'usia', 25,30]);

Upvotes: 2

Related Questions