请帮助我...我正在学习CodeIgniter,我陷入了这个错误。一定有一些愚蠢的错误。 我为此使用xampp软件。 它保持弹出未定义的变量行。CodeIgniter中的未定义变量3
表名是 '用户' 视图文件名是尝试 模型文件名是trydb 控制器文件名是condb
错误
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: rows
Filename: views/try.php
Line Number: 12
Backtrace:
File: C:\xampp\htdocs\ciagain\application\views\try.php
Line: 12
Function: _error_handler
File: C:\xampp\htdocs\ciagain\application\controllers\Welcome.php
Line: 27
Function: view
File: C:\xampp\htdocs\ciagain\index.php
Line: 292
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/try.php
Line Number: 12
Backtrace:
File: C:\xampp\htdocs\ciagain\application\views\try.php
Line: 12
Function: _error_handler
File: C:\xampp\htdocs\ciagain\application\controllers\Welcome.php
Line: 27
Function: view
File: C:\xampp\htdocs\ciagain\index.php
Line: 292
Function: require_once
查看文件
<title>
Connecting data base
</title>
</head>
<body>
<?php
foreach($rows as $r){
echo $r->ID;
echo $r->Name;
}
?>
</body>
</html>
模态文件
<?php
class Site_model extends Model {
function getAll() {
$q = $this->db->get('user');
if($q->num_rows()>0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
?>
控制器文件
<?php
class Site extends CI_Controller {
function index() {
$this->load->model('trydb');
$data['rows'] = $this->trydb->getAll();
$this->load->view('try', $data);
}
}
?>
默认控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function tryagain(){
$this->load->view('try');
}
}
你的模型文件名是'Site_model',你为什么要加载'trydb'? – Dray
@Dray好的谢谢,但仍然没有工作。我需要做一些配置文件夹文件的更改,因为我第一次这样做? –