控制器foreach中的变量在视图内返回NULL

时间:2017-06-05 10:11:46

标签: php codeigniter

我有一个使用codeigniter构建的Web应用程序。我要做的是在我的观点上显示结果摘要。在我的控制器中,我有以下代码

function index() {
    $data['summery'] = $this->Main_model->get_summery();

     foreach ($data['summery'] as $d) {

        $data['total_week']  = $this->Main_model->get_total_week($d['i_id']);

        $data['total_month'] = $this->Main_model->get_total_month($d['i_id']);

    }
}

我拥有的每个夏日记录的问题都有每周总计数和基于i_id的每月计数

在我的视图中,我遍历夏季并显示夏季,但当我echo $total_weekecho $total_month时,它始终返回NULL

$data['total_week']  = $this->Main_model->get_total_week($d['i_id']);
$data['total_month'] = $this->Main_model->get_total_month($d['i_id']);

当我对上面的变量执行var_dump时它显示了数字,但是当我尝试在视图上回显它时它返回NULL

1 个答案:

答案 0 :(得分:3)

我希望我站在正确的地方尝试这样的事情

function index() {
    $this->load->model('main_model');

    $data['summery'] = array();

    $summery = $this->main_model->get_summery();

    foreach ($summery as $d) {

        $data['summery'][] = array(
            'total_week' => $this->main_model->get_total_week($d['i_id']),
            'total_month' => $this->main_model->get_total_month($d['i_id'])
        );

    }

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

查看

<?php foreach ($summery as $something) {?>
    <?php echo $something['total_week'];?>
    <?php echo $something['total_month'];?>
<?php }?>