gpt4 book ai didi

php - CodeIgniter 3 中的 undefined variable

转载 作者:搜寻专家 更新时间:2023-10-30 22:09:45 24 4
gpt4 key购买 nike

请在这方面帮助我...我正在学习 CodeIgniter,但我被这个错误困住了。一定有一些愚蠢的错误。我正在为此使用 xampp 软件。它不断弹出 undefined variable 行。

表名是'user'查看文件名是try模型文件名为 trydb Controller 文件名为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;
}
}


}
?>

Controller 文件

<?php

class Site extends CI_Controller {

function index() {
$this->load->model('trydb');
$data['rows'] = $this->trydb->getAll();

$this->load->view('try', $data);
}

}
?>

默认 Controller

<?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');
}
}

最佳答案

为什么要在模型中加入 foreach 循环?你可以使用这个:

function getAll() {
$q = $this->db->get('user');
$data = $q->result();
return $data;
}

并且按照 DS9 的建议,在 View 文件中使用 below。

if(isset($rows) && sizeof($rows)>0)
{
foreach($rows as $r)
{
}
}

关于php - CodeIgniter 3 中的 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34462748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com