Highcharts的codeigniter-数据无法进入控制器查看

时间:2019-07-18 10:34:36

标签: php codeigniter highcharts

我试图将highcharts库添加到我的CI项目中。所以我在控制器中尝试了数据库查询

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class ChartController extends CI_Controller {

  public function __construct() {

       parent::__construct();

       $this->load->database();

    } 


    public function index()

    {

        $query = $this->db->query("SELECT SUM(numberofclick) as count FROM demo_click 

            GROUP BY YEAR(created_at) ORDER BY created_at"); 

        $data['click'] = json_encode(array_column($query->result(), 'count'),JSON_NUMERIC_CHECK);



        $query = $this->db->query("SELECT SUM(numberofview) as count FROM demo_viewer 

            GROUP BY YEAR(created_at) ORDER BY created_at"); 

        $data['viewer'] = json_encode(array_column($query->result(), 'count'),JSON_NUMERIC_CHECK);



        $this->load->view('my_chart', $data);

    }

}

然后我尝试通过视图访问。我放置了以下代码来检查控制器的输出。

<?php
 echo var_dump($this->_ci_cached_vars);
?>

然后我得到,输出没有值。有什么问题?

array (size=2)
  'click' => string '[]' (length=2)
  'viewer' => string '[]' (length=2)

1 个答案:

答案 0 :(得分:2)

这可能是因为您要返回一组对象而不是数组。由于array_column()期望第一个参数为数组,因此请尝试在每个$query->result()上将$query->result_array()更改为$data

public function index()

{

    $query = $this->db->query("SELECT SUM(numberofclick) as count FROM demo_click 

        GROUP BY YEAR(created_at) ORDER BY created_at"); 

    $data['click'] = json_encode(array_column($query->result_array(), 'count'),JSON_NUMERIC_CHECK);



    $query = $this->db->query("SELECT SUM(numberofview) as count FROM demo_viewer 

        GROUP BY YEAR(created_at) ORDER BY created_at"); 

    $data['viewer'] = json_encode(array_column($query->result_array(), 'count'),JSON_NUMERIC_CHECK);



    $this->load->view('my_chart', $data);

}