Codeigniter数据库值未在视图中显示

时间:2015-10-20 14:40:59

标签: php codeigniter 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>  

https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx

3 个答案:

答案 0 :(得分:2)

更改您的控制器

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

答案 1 :(得分:0)

这是错误的

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

要么是

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

这应该有所帮助。而且

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

答案 2 :(得分:0)

在您的模型中,您错过了?来电中的return

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