Search code examples
phpgridviewyiiyii2

Getting data from a column in GridView Yii2


How do I get data from a specific column in GridView in Yii2 and it will be shown into the Google Chart

I used this GoogleChart Yii library: https://github.com/ScottHuangZL/yii2-google-chart

I was able to show a sample data:

<div class="col-sm-5">
        <?php
        use scotthuangzl\googlechart\GoogleChart;

        echo GoogleChart::widget(array('visualization' => 'PieChart',
            'data' => array(
                array('Task', 'Hours per Day'),
                array('Work', 11),
                array('Eat', 2),
                array('Commute', 2),
                array('Watch TV', 2),
                array('Sleep', 7)
            ),
            'options' => array('title' => 'My Daily Activity')));

Solution

  • You should write query to retrieve data. I suggest you to use Active Record.

     $data = YourModel::find()->select('column_name')->asArray()->all();
    

    Query will return multi-minesional array, you have to reduce to one dimensional with array_map function

    $data = array_map('current',$data);
    

    At last but not east, send $data to your view. You can divide your array as you need.