0

PHP エラーが発生しました

Severity: Notice
Message: Undefined variable: data
Filename: views/body_view.php
Line Number: 13
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/body_view.php
Line Number: 13

だから私はCodeigniterでこの問題を抱えています

コントローラ:

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

class Blog extends CI_Controller {

    public function __construct()
    {
    parent::__construct();
    }    
    public function index()
    {
        echo 'Hello World!';
    }

    public function mat()
    {
        $this->load->model('materials_model');
        $data = array();
        $data['news'] = $this->materials_model->get();
        $this->load->view('body_view',$data);
    }


}
?>

モデル:

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

class Materials_model extends CI_Model
{
    public function get()
    {
        $query = $this->db->get('materials');
        return $query->result_array();
    }
}

?>

意見:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="The Sabotage Rebellion hackers" />

    <title>a</title>
</head>

<body>

<?php foreach ($data as $one):?>
<?=$one['author']?>
<?php endforeach; ?>

</body>
</html>
4

3 に答える 3

1

ビューコードを次のように変更します

<?php foreach ($news as $one):?>
<?=$one['author']?>
<?php endforeach; ?>

つまり$data$news

于 2012-08-06T12:25:31.457 に答える
0

ビューでは、次のようにします

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="The Sabotage Rebellion hackers" />

    <title>a</title>
</head>

<body>

<?php foreach ($news as $one):?>
<?=$one['author']?>
<?php endforeach; ?>

</body>
</html>

foreach で $data を $news に置き換えます

于 2012-08-06T12:26:00.093 に答える
0

次のよう$dataな配列があるとします。

$data['x'] = 1;

$dataをビューに渡すと、

1as$xと not でアクセスできます$data['x']

于 2012-08-06T12:27:42.707 に答える