CodeIgniter变量数据/值未从控制器传递到视图


CodeIgniter variable data/ value not passing to the view from the Controller

我需要在CodeIgniter视图文件中回显一个变量值。但当我用。。。

<?php echo $highcharts; ?>

在视图的第108行,它给出了一个错误。请帮我回显这个值,这样我就可以在HighCharts中使用它。

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: highcharts
Filename: reports/bqt_daily_income_view.php
Line Number: 108

这是我的控制器:

function bqt_daily_income() {
    $json_data = array();
    $query = $this->db->query("SELECT time_stamp,  SUM(paid) AS income  FROM payments WHERE type = 'Banquet Reservation' GROUP BY time_stamp ORDER BY time_stamp ASC LIMIT 30");
    if ($query->num_rows() > 0) {
    foreach($query->result_array() as $row) {
    $json_data['data'][] = (int) $row['income'];
        }
    }
    $highcharts = json_encode($json_data);
    //return $json_data;
    //print_r($json_data); // Array ( [data] => Array ( [0] => 1700 [1] => 5000 ) ) 
    //print_r($highcharts); // {"data":[1700,5000]}
    $this->load->view('reports/bqt_daily_income_view', $highcharts);

试试看:

$this->load->view('reports/bqt_daily_income_view', array('highcharts' => $highcharts));

视图文件:

echo $highcharts;

备选张贴方式:

$this->data['highcharts'] = json_encode($json_data);
$this->data['other']      = "bla bla bla";
$this->load->view('reports/bqt_daily_income_view', $this->data);

视图文件:

echo $highcharts;
echo $other;

我认为您需要:

$data['highcharts'] = json_encode($json_data);
$this->load->view('reports/bqt_daily_income_view', $data);

您应该将数据数组传递给视图,数组的键将成为视图中可访问的变量

function bqt_daily_income() {
    $json_data = array();
    $query = $this->db->query("SELECT time_stamp,  SUM(paid) AS income  FROM payments WHERE type = 'Banquet Reservation' GROUP BY time_stamp ORDER BY time_stamp ASC LIMIT 30");
    if ($query->num_rows() > 0) {
    foreach($query->result_array() as $row) {
    $json_data['data'][] = (int) $row['income'];
        }
    }
    $highcharts['highcharts'] = json_encode($json_data);
    //return $json_data;
    //print_r($json_data); // Array ( [data] => Array ( [0] => 1700 [1] => 5000 ) ) 
    //print_r($highcharts); // {"data":[1700,5000]}
    $this->load->view('reports/bqt_daily_income_view', $highcharts);
}

在视图

<?php echo print_r(json_decode($highcharts)); ?>