试图获得非对象的属性是我在编码器中得到的错误


Trying to get property of non object is the error what iam getting in codeigniter

我想显示从控制器传递到视图页面的单个值,但当我尝试这样做时,我得到错误试图获得非对象的属性,所以请求ci开发人员帮助我

这是我的控制器页面

 $data['showdata']  =   $this->searchresultss->login($per_page,$look,$age, $age_to,$age_from,$se_ct,$subsect,$coun_try,$sta_te, $ci_ty,$qualification);
        $this->load->view('searchresult',$data);
**This is my view page**
<?php
echo "<pre>";
print_r($showdata);
  if (isset($showdata)){
            foreach ($showdata as $key) {
?>
<?php echo($key->gender); ?>// This is the line where iam getting error
  <?php
            }
        }
        ?>
**Here is my model page**
<?php
Class Searchresultss extends CI_Model
{
 function login($per_page=3,$look,$age,$age_to,$age_from,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification)
 {

$query="SELECT *
FROM users
";
$data=array();
$query=$this->db->query($query);
$data['results']=$query->result_array();
$data['count']=$query->num_rows();
$data['pages']=ceil($data['count']/3);
return $data;
 }
}

我得到错误,试图获得非对象的属性。我试图打印从控制器传递到视图页面的单个值。但是我不能这样做。

您没有在控制器中加载模型。在调用模型函数之前,在控制器中加载模型searchresultss。如,

$this->load->model('searchresultss');

改变echo($key->gender);

echo $key['gender'];

这将解决这个问题

你的echo没有错。

但是我猜你没有接受你的查询结果,所以:

try this:

  if (isset($showdata)){
  foreach ($showdata->result() as $key){ // just add ->result() after $showdata
  echo($key->gender); ?>
  }
  }