CodeIgniter PHP未定义变量错误
我一直在为最后2个小时努力寻找为什么我得到这个错误,但我仍然卡住!我已经在这里检查了答案,但仍然没有。CodeIgniter PHP未定义变量错误
检查类似的线程后,我似乎无法找到一个解决方案
帮助将不胜感激
错误信息:遇到
一个PHP错误
严重性:注意
消息:未定义的变量:UPGRADES
文件名:观点/ test.php的
行号:207
型号:
<?php
if(!defined('BASEPATH')) exit('Hacking Attempt: Get out of the system ..!');
class M_live extends CI_Model {
function M_live(){
parent::Model();
function getUpgrades(){
$this->db->select("*");
$this->db->from('client_trust_versions')
$query = $this->db->get();
return $query->result_array();
}
}
}
?>
控制器:
<?php
if(!defined('BASEPATH')) exit('Hacking Attempt: Get out of the system ..!');
class C_Live extends CI_Controller
{
function C_Live(){
parent::__construct();
$this->load->model('M_live');
}
public function index(){
$query = $this->M_live->getUpgrades();
$data['UPGRADES'] = null;
if($query){
$data['UPGRADES'] = $query;
}
$this->load->view('Test', $data);
}
}
?>
查看:
<table>
<tr>
<th>ID</th>
<th>Client Name</th>
<th>App Server</th>
<th>Instance Name</th>
<th>BankStaff Server</th>
<th>SQL Server</th>
<th>Instance</th>
<th>Health Roster Version</th>
<th>Employee Online Version</th>
<th>Roster Perform</th>
<th>BankStaff Version</th>
<th>SafeCare Version</th>
<th>E-Expenses</th>
</tr>
<?php foreach((array)$UPGRADES as $upgrades){?>
<tr><td><?php=$upgrade->ID;?></td>
<td><?php=$upgrade->Client_Name;?></td>
<td><?php=$upgrade->App_Server;?></td>
<?php }?>
型号:
<?php
if(!defined('BASEPATH')) exit('Hacking Attempt: Get out of the system ..!');
class M_live extends CI_Model
{
function M_live()
{
parent::Model();
}
function getUpgrades()
{
$query = $this->db->get('client_trust_versions');
// Produces: SELECT * FROM client_trust_versions
return $query->num_rows() > 0 ? $query->result() : NULL;
}
}
如果发现行,这将返回的结果作为对象,并返回NULL,如果没有行。更改为返回一个对象而不是数组,因为这是您在具有$upgrade->key
语法的视图中使用的类型。要使用数组,你会使用$upgrade['key']
在控制器使用这种
public function index(){
$query = $this->M_live->getUpgrades();
if(isset($query)){
$data['upgrades'] = $query;
}
$this->load->view('Test', $data);
}
新视角:
<table>
<tr>
<th>ID</th>
<th>Client Name</th>
<th>App Server</th>
<th>Instance Name</th>
<th>BankStaff Server</th>
<th>SQL Server</th>
<th>Instance</th>
<th>Health Roster Version</th>
<th>Employee Online Version</th>
<th>Roster Perform</th>
<th>BankStaff Version</th>
<th>SafeCare Version</th>
<th>E-Expenses</th>
</tr>
<?php
if(isset($upgrades)){
//If no rows were found $upgrades will be NULL and isset() will return false.
//Therefore, you never try to address an undefined variable.
foreach($upgrades as $upgrade){?>
<tr><td><?php=$upgrade->ID;?></td>
<td><?php=$upgrade->Client_Name;?></td>
<td><?php=$upgrade->App_Server;?></td></tr>
<?php }}?>
</table>
通知$ UPGRADES改为$升级。公约是所有大写字母名称都表示一个常数。请注意0中复数和单数var名称的用法,它避免了您可能遇到的名称冲突。 (并因此全部上限var名称)
这很好,它的工作,我知道错误。只需要获取屏幕上显示的数据。再次感谢你的帮助! –
修正模型代码:
<?php
if(!defined('BASEPATH')) exit('Hacking Attempt: Get out of the system ..!');
class M_live extends CI_Model {
function M_live(){
parent::Model();
}
function getUpgrades(){
$this->db->select("*");
$this->db->from('client_trust_versions')
$query = $this->db->get();
return $query->result_array();
}
}
?>
我是否使用我在控制器和视图中使用的相同代码?我也在努力让数据在我看来表现出来。你可以帮忙的事情? –
那么你可以使用@Dfriend回答.. – Nere
可能重复[PHP:“注意:未定义的变量”和“注意:未定义的索引”](http://stackoverflow.com/questions/4261133/php-notice-undefined -variable-and-notice-undefined-index) – Qirel
那么我猜查询不会返回结果。要测试用$ data ['UPGRADES'] = array();'替换'$ data ['UPGRADES'] = null;'它应该可以工作 – Steve
如果你看一下变量名,你可以使用'foreach'语句中'$ upgrades',但里面有'$ upgrade'。 – Qirel