2012-11-09 56 views
0

我有foloowing控制器(supernavigationloggedin):查询与View传递笨

<?php 

class Supernavigationloggedin extends CI_Controller { 
    function index(){ 
     #get current session id 
      $currentSessionID = $this->session->userdata('session_id'); 
     #get all the account row for the given sessionID 
      $data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row(); 
     #views 
      $this->load->view('supernavigationloggedin',$data); 
    } 
} 


?> 

,并命名为下面的视图(supernavigationloggedin):

<div id="superNavigation"> 
    <h5><strong>Welcome</strong>, <?php $info->fname; ?>&nbsp;<a href="#">Account Settings</a></h5> 
    <div class="clearL"> </div> 
</div 

>

它不断抛出在线错误:<h5><strong>Welcome</strong>, <?php echo $info['fname']; ?>&nbsp;<a href="#">Account即消息:>>>Trying to get property of non-object

我试过:<?php echo $info['fname']; ?> <?php echo $info->fname; ?>但似乎都没有工作。

回答

1

这是因为$info是空的,没有数据库请求。你如果你希望你的查询返回的对象要做到这一点是这样的:

$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row(); 

还是这样,如果你喜欢把它变成一个数组:

$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row_array(); 

这样,它应该工作。 row()row_array()是执行您的查询所必需的。

+0

感谢您的帖子。所以,在我看来,我输出<?php $ info ['fname']; ?>是吗? – CodeTalk

+0

如果你使用我的答案的第一个查询,你使用'$ info-> fname',如果你使用seconth,你可以使用'$ info ['fname']'。我建议你在你的视图中使用'print_r($ info)'来查看它包含的内容。 –

+0

但是这个$ info-> fname是否需要echo $ info-> fname? – CodeTalk