0

我想在条形图中显示数据库表中的数据。我使用了 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;

}

4

1 回答 1

0

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

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');}
}
于 2013-08-12T08:26:39.460 回答