2017-09-29 49 views
0

我想呼應我此行的名單,當我試圖print_r的值可以顯示,但得到的數據,當我回應的結果總是PHP-笨如何在會議

消息:未定義的變量:冒險

文件名:視圖/ profile.php

行號:121

回溯:

Severit Y:警告

消息:的foreach()

文件名提供參數無效:視圖/ profile.php

行號:121

這是我的控制器:

public function getListTrip(){   
    $this->load->model('userModel');   
    $data['adventure'] = $this->userModel->getTrip()->result_array(); 
    //echo ($data); 
    $this->load->view('profile', $data);   
} 

這是我的型號:

function getTrip(){ 
    $userId = $this->session->userdata('user_id');  
    return $this->db->get_where('adventure',['user_id' => $userId]); 
} 

這是我的看法

     <table> 
          <?php 
          foreach ($adventure as $b){ 
           echo "<tr> 
           <td>$b->name</td> 
           <td>$b->place</td> 
           <td>$b->category</td> 

           </tr>"; 
          } 

         ?> 
         </table> 

所以我應該如何修改我的代碼,以使值顯示在我的頁面絲毫呼應的foreach沒有print_r的...非常感謝

+0

提示你命名你的控制器錯誤的CI請點擊此處閱讀關於文件和類命名在CI https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming – user4419336

回答

1

更改模型

function getTrip(){ 
    $userId = $this->session->userdata('user_id');  
    $query= $this->db->get_where('adventure',['user_id' => $userId]); 
    return $query->result(); 
} 

而且chnage在您的控制器

$data['adventure'] = $this->userModel->getTrip()->result_array(); 

$data['adventure'] = $this->userModel->getTrip(); 
+0

其工作傢伙,謝謝..但我應該看到這在我的控制器/ getListTrip無法看到我的/配置文件 –

1

echo用於輸出一個或多個字符串,因爲你的$data被你看到錯誤的數組,你需要傳遞的數據查看和重複它在視圖本身,如:

public function getListTrip(){ 
    $this->load->model('userModel'); 
    $data['adventure'] = $this->userModel->getTrip()->result_array(); 
    //pass data to your view 
    $this->load->view('yourviewname', $data); 
} 

欲瞭解更多信息,請查看Codeigniter Views

更新

檢查,如果你的陣列試圖重複通它,就像在你看來::

<?php 
if(!empty($adventure)) { 
    foreach($adventure as $b): 
?> 
     <tr> 
      <td><?php echo $b['name']; ?></td> 
      <td><?php echo $b['place']; ?></td> 
      <td><?php echo $b['category']; ?></td> 
     </tr> 
<?php 
    endforeach; 
} 
?> 
+0

我嘗試了你的建議,並且在我的查看頁面中仍然出現了一些錯誤,請幫助我在哪裏錯誤?? –

+0

@FahmiSalmin檢查添加的代碼。順便說一句,因爲reult是數組,所以你不能使用' - >'操作符來訪問這些字段。 –

0

你不能前不爲空回顯數組的內容。

所以每當你想查看一個數組的內容使用print_r($arrayName);

,當你只想打印任何變量只是使用echo $variableName;

0

我沒有看到您的代碼有任何問題。如果你不使用自動加載器加載會議庫,可能是您的問題:

$this->load->library('session'); 

您可以查看文檔Codeigniter sessions