2012-09-24 121 views
0

這是我的代碼。我想在CodeIgniter中從數據庫中獲取數據,但得到錯誤

控制器功能

function index() 
{ 
     $data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message'); 

     $this->load->model('customers_model'); 
     $data['rows'] = $this->customers_model->getAll(); 

     $meta['page_title'] = 'Customers'; 
     $data['page_title'] = 'Customers'; 
     $this->load->view('../header', $meta); 
     $this->load->view('content', $data); 
     $this->load->view('../footer'); 
} 

型號功能

public function getAll() 
{ 
     $q = $this->db->get('customers'); 

     if($q->num_rows() > 0) { 
      foreach ($q->result() as $row) { 
       $data[] = $row; 
      } 

      return $data; 
     } 
} 

查看文件

<table cellpadding=0 cellspacing=10 width="100%"> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Company</th> 
      <th>Phone</th> 
      <th>Email</th> 
      <th>Address</th> 
      <th>City</th> 
      <th>Code</th> 
      <th>State</th> 
      <th>Country</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php foreach ($data as $row):?> 
      <tr> 
       <td><?php echo $row->name; ?></td> 
       <td><?php echo $row->company; ?></td> 
       <td><?php echo $row->phone; ?></td> 
       <td><?php echo $row->email; ?></td> 
       <td><?php echo $row->address; ?></td> 
       <td><?php echo $row->city; ?></td> 
       <td><?php echo $row->postal_code; ?></td> 
       <td><?php echo $row->state; ?></td> 
       <td><?php echo $row->country; ?></td> 
      </tr> 
     <?php endforeach;?> 
    </tbody> 
</table> 

我收到錯誤

A PHP Error was encountered 
Severity: Notice 
Message: Undefined variable: data 
Filename: views/content.php 

A PHP Error was encountered 
Severity: Warning 
Message: Invalid argument supplied for foreach() 
Filename: views/content.php 

我第一次使用CI,不知道我做錯了什麼。請幫忙。

回答

2

您在關聯數組中爲模型返回的數據分配的密鑰爲rows。在視圖中,變量的名稱是$rows而不是$data

+0

非常感謝您的幫助... – Saleem

相關問題