遇到 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>