未显示在视图中的Codeigniter数据库值
问题描述:
我遇到了一个问题,即我的数据库中的数据未显示在我的视图中。据我所知,我的控制器,型号和视图设置正确。我之前完成了这个工作,所以我知道需要什么,但是我找不到我的旧文件。我希望你们能帮忙?未显示在视图中的Codeigniter数据库值
控制器:
<?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();
if(isset($query)){
$data['upgrades'] = $query;
}
$this->load->view('Test', $data);
}
}
?>
型号:
<?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('live_client_trust_versions');
// Produces: SELECT * FROM client_trust_versions
return $query->num_rows() > 0 $query->result() : NULL;
}
}
?>
查看:
<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>
<td><?php=$upgrade->Instance_Name?></td>
<td><?php=$upgrade->BankStaff_Server?></td>
<td><?php=$upgrade->SQL_Server;?></td>
<td><?php=$upgrade->Instance;?></td>
<td><?php=$upgrade->Health_Roster_Version;?></td>
<td><?php=$upgrade->Employee_Online_Version;?></td>
<td><?php=$upgrade->Roster_Perform_Version;?></td>
<td><?php=$upgrade->BankStaff_Version;?></td>
<td><?php=$upgrade->SafeCare_Version;?></td>
<td><?php=$upgrade->E-Expenses;?></td>
</tr>
<?php }}?>
</table>
答
变化控制器
class C_Live extends CI_Controller
{
function __construct(){
parent::__construct();
$this->load->model('M_live', '', TRUE);
}
public function index(){
$data['upgrades'] = $this->M_live->getUpgrades();
echo "<pre>";
print_r($data['upgrades']);
$this->load->view('Test', $data);
}
}
和型号
<?php
if(!defined('BASEPATH')) exit('Hacking Attempt: Get out of the system ..!');
class M_live extends CI_Model
{
function __construct()
{
parent::Model();
}
function getUpgrades()
{
// $query = $this->db->get('live_client_trust_versions');
// Produces: SELECT * FROM client_trust_versions
//return $query->num_rows() > 0 $query->result() : NULL;
$this->db->select('*');
$this->db->from('live_client_trust_versions');
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
?>
答
这是错误的
<?php=$upgrade->ID;?>
要么应该是
<?php echo $upgrade->ID;?> or <?= $upgrade->ID;?>
这应该帮助。而且还
return $query->num_rows() > 0 ? $query->result() : NULL;
^is missing.
答
在你的模型,你已经在你的return
通话错过了?
:
return $query->num_rows() > 0 ? $query->result() : NULL;
// Missing here^
阅读这个http://w3code.in/2015/ 10 /如何插入和选择数据从数据库中codeigniter初学者指南/ – Ricky