带有CodeIgniter的HighCharts对MySQL获取的数据没有任何结果

时间:2013-08-12 08:18:07

标签: php ajax json codeigniter highcharts

我想在条形图中显示数据库表中的数据。我使用了HighCharts库。该图表显然加载了默认模板数据。但后来我做了一些更改,以便它可以代表数据库中的数据。虽然我认为逻辑流程是正确的,但它现在根本没有显示任何东西。仅供参考:我对JSON和AJAX的了解很多。

查看:

<script type="text/javascript">

$(document).ready(function() {  

Highcharts.setOptions({
colors: ['#e74c3c']
});  

$(function () {
$('#container').highcharts({
    chart: {
        type: 'bar'
    },
    title: {
        text: 'Daily Income'
    },
    xAxis: {
        categories: ['2013-08-10', '2013-08-11', '2013-08-12']
    },
    yAxis: {
        title: {
            text: 'Daily Income From Banquet Reservations For Last 30 Days'
        }
    },
    series: <?php echo $json; // echo encoded data ?>
    }],
});
});

});

</script>

...

<div id="container" style="width:100%; height:400px;"></div>

控制器:

    function bqt_daily_income() {
    $this->load->model('reports_model');
    $this->load->model('json_model'); // load model

    $report['query'] = $this->reports_model->bqt_daily_income();
    $table['income'] = $this->json_model->bqt_daily_income(); // fetch data from mysql database 
    $json['table'] = json_encode($table); // encode to json
    $data = array_merge($report, $json); // merge with other arrays as i also have a html table with raw data from database

    $role = $this->session->userdata('role');
    if ($role === 'admin') { $this->load->view('reports/bqt_daily_income_view', $data); } // load view with data array (merged)
    else if ($role === 'manager') { $this->load->view('error'); }
    else if ($role === 'user') { $this->load->view('error'); }
    else { $this->load->view('login');}
}

型号:(json_model)

function bqt_daily_income($json_data = array()) {
  $query = $this->db->query("SELECT DATE(time_stamp) AS day,  SUM(paid) AS income FROM payments WHERE type = 'Banquet Reservation' GROUP BY day ORDER BY time_stamp DESC LIMIT 30");
  if ($query->num_rows() > 0) {
     foreach($query->result_array() as $row) {
     $json_data[$row['day']] = $row['income'];
     }
  }
  return $json_data;

}

1 个答案:

答案 0 :(得分:0)

在您的控制器中进行以下更改,我认为您将会很高兴。有关所做的更改,请参阅评论 EDITED

function bqt_daily_income() {
    $this->load->model('reports_model');
    $this->load->model('json_model'); // load model

    $report['query'] = $this->reports_model->bqt_daily_income();
    $table['income'] = $this->json_model->bqt_daily_income(); // fetch data from mysql database 
    $data['json']   = json_encode($table['income']); // CHANGES HERE FOR THE JS
    echo "<pre>";print_r($data['json']); echo "</pre>";die;  //TO SEE IF THE JSON IS RETURNING CORRECTLY FROM THE MODEL OR NOT
    $data['merged'] = array_merge($report, $json); // NOW IN VIEW USE $merged FOR THE MERGED ARRAY
    echo "<pre>";print_r($data['json']); echo "</pre>";die;  //TO SEE IF THE MERGED IS OKAY OR NOT
    $role = $this->session->userdata('role');
    if ($role === 'admin') { $this->load->view('reports/bqt_daily_income_view', $data); } // load view with data array (merged)
    else if ($role === 'manager') { $this->load->view('error'); }
    else if ($role === 'user') { $this->load->view('error'); }
    else { $this->load->view('login');}
}