2017-05-06 160 views
0

我想製作一個圖表,我有數據庫省份,並且我想將它作爲Highcharts上的xAxis。查詢從數據庫中獲取數據更改爲數組

我的數據庫

My Database

<?= \dosamigos\highcharts\HighCharts::widget([ 
'clientOptions' => [ 
    'chart' => [ 
      'type' => 'spline' 
    ], 
    'title' => [ 
     'text' => 'Fruit Consumption' 
     ], 
    'xAxis' => [ 
     'categories' => [ 
     'Jabodetabek', 
     'Banten', 
     'Jawa Tengah', 
     'Jawa Timur', 
     'Medan', 
     'Riau', 
     'Bangka Belitung', 
     'Lampung', 
     'Kalimantan Selatan', 
     'Kalimantan Barat', 
     'Kalimantan Timur', 
     'Kalimantan Tengah', 
     'NTB', 
     'NTT', 
     'Papua'] 
    ], 
    'yAxis' => [ 
     'title' => [ 
      'text' => 'Fruit eaten' 
     ] 
    ], 
    'plotOptions' => [ 
     'line' => [ 
      'dataLabels' => [ 
       'enabled'=> true 
      ] 
     ] 
    ], 
    'series' => [ 
     ['data' => [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 
    ], 

    'responsive' => [ 
     'rules' => [[ 
      'condition' => [ 
       'maxWidth' => 500 
      ], 
      'charOptions' => [ 
       'legend' => [ 
        'align' => 'center', 
        'verticalAlign' => 'bottom', 
        'layout' => 'horizontal' 
       ], 

       'yAxis' => [ 
        'labels' => [ 
         'align' => 'left', 
         'x' => 0, 
         'y' => 0 
        ], 
        'title' => [ 
         'text' => null 
        ] 
       ] 
      ] 
     ]] 
    ] 
] 

]);

如何使省表的內容爲數組?我希望所有數據都由各省統計。
請幫幫我

+0

如果您選擇使用PDO,則可以使用http://php.net/manual/fr/pdostatement.fetchall.php將結果集轉換爲數組 – Paul

回答

0

yii2友好的方法是使用gii爲每個表格生成模型。然後你可以得到一個像

$provinces = \app\models\Province::find()->select('name')->asArray(); 

你可以使用類似的技術來獲得計數;我會發佈一個例子,除了我不知道什麼是正確的SQL將是你的情況。