我在 CI 中有这个错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: controllers/First.php
Line Number: 33
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: views/welcome_view.php
Line Number: 1
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/welcome_view.php
Line Number: 1
有我的控制器:
<?php
if ( ! defined('BASEPATH')) exit ('No direct script access allowed');
class First extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('materials_model');
}
public function index()
{
$this->load->view('header_view');
$this->load->view('menu_view');
$this->load->view('about_me_view');
$this->load->view('navigation_view');
$this->load->view('search_view');
$this->load->view('main_text_view');
$this->load->view('footer_view');
}
public function mat()
{
$this->load->model('materials_model');
$this->materials_model->get();
$data['news'] = $this->materials_model->get();
$this->load->view('welcome_view',$news);
if(empty($data['news']))
{
echo'Array is Null';
}
else
{
echo'Array has info';
}
}
我的模型:
<?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();
}
}
?>
我的观点 :
<?php foreach ($news as $one):?>
<?=$one['author']?>
<?php endforeach; ?>
我知道,我传递到视图中的那个数组不是 NULL(我检查它的 print_r 和 if,否则构造),但我可以传递它并查看它。我有什么错误?