CodeIgniter变量数据/值未从Controller传递到视图

时间:2013-08-12 11:45:49

标签: php codeigniter

我需要在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);

3 个答案:

答案 0 :(得分:3)

尝试:

$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;

答案 1 :(得分:1)

我认为你需要:

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

答案 2 :(得分:1)

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

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)); ?>