0

首先,请考虑我是 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>
4

1 回答 1

2

$info加载视图时,Codeigninter 会分解变量。它应该是一个数组:

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

然后在你看来,你可以做得到echo $key输出value

于 2013-06-17T17:47:03.927 回答