将PHPExcel与CodeIgniter集成时的未定义变量

时间:2016-06-03 12:22:34

标签: php codeigniter

我是OOP和CodeIgniter的新手。一天前开始使用页面控制器和页面的基本示例。现在我需要与PHPExcel集成。这是我的控制器:

<?php

class Report extends CI_Controller {
    public function __construct() {
        parent::__construct();
    }

    public function readReport() {
        $file = APPPATH."/third_party/teste.xlsx";
        //load the excel library
        $this->load->library('excel');
        //read file from path
        $objPHPExcel = PHPExcel_IOFactory::load($file);
        //get only the Cell Collection
        $cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
        //extract to a PHP readable array format
        foreach ($cell_collection as $cell) {
            $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
            $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
            $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
            //header will/should be in row 1 only. of course this can be modified to suit your need.
            if ($row == 1) {
                $header[$row][$column] = $data_value;
            } else {
                $arr_data[$row][$column] = $data_value;
            }
        }
        //send the data in an array format
        $data['teste'] = "Rodrigo";
        $data['header'] = $header;
        $data['values'] = $arr_data;
        $this->load->view('pages/home', $data);
        }
}

以下是我的观点:

<!-- Page Content -->
<div id="page-content-wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <?php echo $teste; ?>
            </div>
        </div>
    </div>
</div>
<!-- /#page-content-wrapper -->

页面控制器:

<?php

class Pages extends CI_Controller {
    public function view ($page = 'home') {
        if (!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
            show_404();
        }

        $data['title'] = str_replace("_", " ", $page);

        $this->load->helper('url');
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }
}

错误讯息:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: teste

Filename: pages/home.php

Line Number: 6

Backtrace:

File: C:\xampp\htdocs\PhpProject3\application\views\pages\home.php
Line: 6
Function: _error_handler

File: C:\xampp\htdocs\PhpProject3\application\controllers\Pages.php
Line: 13
Function: view

File: C:\xampp\htdocs\PhpProject3\index.php
Line: 315
Function: require_once

有人可以帮助我在这里做错了吗?

2 个答案:

答案 0 :(得分:1)

在页面控制器中,您没有$ data ['teste']变量。仅从页面控制器访问变量。

答案 1 :(得分:1)

您尚未在页面控制器中声明teste变量,您需要在评论中将其声明为@Saty,或者您需要在视图文件中打印前使用isset,如下所示

<?php if(isset($teste)) {
        echo $teste; 
    }
?>