2015-10-20 55 views
1

我遇到了一個問題,即我的數據庫中的數據未顯示在我的視圖中。據我所知,我的控制器,型號和視圖設置正確。我之前完成了這個工作,所以我知道需要什麼,但是我找不到我的舊文件。我希望你們能幫忙?未顯示在視圖中的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> 

The result I'm getting in my view.

+0

閱讀這個http://w3code.in/2015/ 10 /如何插入和選擇數據從數據庫中codeigniter初學者指南/ – Ricky

回答

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

我試過你有什麼建議,但我的表仍未顯示。 –

+0

我已經更新了我的答案,現在再試一次 – Ricky

+0

我真的很難過成爲一個痛苦,但它仍然沒有顯示數據是視圖。我完全不知道它可能是什麼。 –

0

這是錯誤的

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

要麼應該是

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

這應該幫助。而且還

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

在你的模型,你已經在你的return通話錯過了?

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