2017-09-29 50 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