代码编写器数据库值未显示在视图中


Codeigniter Database Values Not Showing In View

我遇到了一个问题,来自数据库的数据没有显示在我的视图中。据我所知,我的控制器,模型和视图设置正确。我以前这样做过,所以我知道需要什么,但我找不到我的旧文件。我希望你们能帮忙?

控制器:

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

在您的模型中,您在return呼叫中错过了?:

return $query->num_rows() > 0 ? $query->result() : NULL;
              // Missing here ^

这是错误的

<?php=$upgrade->ID;?>

应该是

<?php echo $upgrade->ID;?> or  <?= $upgrade->ID;?>

这应该有帮助。还有

return $query->num_rows() > 0 ? $query->result() : NULL;
                              ^ is missing.