///DATA MODEL
class Data extends CI_Model{
function __construct() {
parent::__construct();
}
function read_record(){
$q = $this->db->get('category');
return $q->result();
}
}
//in Controller
class Test_admin extends CI_Controller{
function index(){
$this->load->model('data');
$dat = array();
if( $q = $this->data->read_record()){
$dat['record'] = $q;
}
$this->load->view('test', $dat);
}
}
// in view
<?php
foreach ($record as $row){
echo $row->name;
echo '<br />';
}
?>
显示错误:
遇到PHP错误
严重性:注意
消息:未定义的属性:stdClass :: $ name
文件名:views / test.php
行号:35
回溯:
文件:C:\ wamp \ www \ ci312 \ application \ views \ test.php 行:35 功能:_error_handler
文件:C:\ wamp \ www \ ci312 \ application \ controllers \ test_admin.php 行:9 功能:查看
文件:C:\ wamp \ www \ ci312 \ index.php 行:315 功能:require_once
答案 0 :(得分:-1)
在视图代码中添加var_dump()以检查传入的内容...
视野
<?php
var_dump($record); // Debug
exit(); // Debug
foreach ($record as $row){
echo $row->name;
echo '<br />';
}