为什么我的观点不接收变量?

时间:2013-06-17 17:40:58

标签: php codeigniter codeigniter-2

首先,请考虑我是CodeIgniter的新手。

当我做print_r($info);

内部控制器:Array ( [message] => Usu. [value] => 1 )

内部视图:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: info

Filename: views/administration.php

Line Number: 4

在我的控制器内部,我试图改变以下几行:

        $this->load->view('administration', (object) $info);
        $this->load->view('administration', $info);
        $this->load->view('administration');

控制器:

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('session');
    }

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

        //Load admin model
        $this->load->model('administration_model');

        //Afficher l'administration
        $info = $this->administration_model->log();
        print_r($info);  // Array ( [message] => Usu. [value] => 1 )
        $this->load->view('administration', $info);
    }

}

查看:

<?php
    $this->load->helper('form');
    $title  =   "Administration";
    $error  =   $info["message"];
    $output = "";

    if($info["value"]){
        $noticeClass    =   "goodNotice";
    }else{
        $noticeClass    =   "errorNotice";

        $output .= form_open('admin');

        $output .= '<h5>Username</h5>';
        $output .= '<input type="text" name="username" value="" size="50" />';

        $output .= '<h5>Password</h5>';
        $output .= '<input type="password" name="password" value="" size="50" />';

        $output .= '<div><input type="submit" value="Submit" /></div>';

        $output .= '</form>';
    }
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?php echo $title; ?></title>
</head>
<body>
    <h3 class='<?php echo $noticeClass ?>'><?php echo $error; ?> </h3>

    <?php echo $output; ?>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

Codeigninter在加载视图时会展开$info变量。它应该是一个数组:

$info = array('key' => 'value');

然后在您看来,您可以echo $key输出value