【问题标题】:Trying to get property of non-object error using codeigniter尝试使用 codeigniter 获取非对象错误的属性
【发布时间】:2016-09-10 19:14:33
【问题描述】:

我对 codeigniter 有疑问。这是消息

遇到 PHP 错误

严重性:通知

消息:试图获取非对象的属性

文件名:员工/detail.php

行号:15

我有这样的模型:

public function getByID($id){
    $this->db->select('*');
    $this->db->from('master_employee me'); 
    $this->db->join('master_position mp', 'mp.mp_id=me.mp_id');
    $this->db->where('me.me_id', $id);
    $query = $this->db->get();
    return $query->row();
}

控制器:

public function edit($id){
    /**
     * [$data get data from database]
     * @var array
     */
    $data = array($id);
    $data['msg']            = $this->_get_flashdata();
    $data['rows']           = $this->m_employee->getByID($id);
    $data['position']       = $this->m_position->get();

    /**
     * [$html call all wireframe]
     * @var array
     */
    $html = array();
    $html['header']     = $this->load->view('admin/header',$data,true);
    $html['kiri']       = $this->load->view('admin/kiri',null,true);
    $html['content']    = $this->load->view('admin/employee/edit',$data,true);
    $this->load->view('admin/template',$html);
}

并像这样查看

<?php if($rows->me_photo == NULL): ?>
<img src="<?php echo base_url('/upload/be/employee/default-no-image.png'); ?>" class="img-responsive" title="no-photo" style="margin-bottom:10px" />
<?php else: ?>
<img src="<?php echo base_url('/upload/be/employee'.$rows->me_name); ?>" class="img-responsive" style="margin-bottom:10px" />
<?php endif; ?>

如何解决我的问题?请

【问题讨论】:

  • 您是否遵循Some_model.phpclass Some_model extends CI_Model{} 以及Some_other_class.phpSome_other_class extends CI_Controller{} 中的名称标准化和样式指南规则,如documentations 中所述?在版本 3+ 中更重要(注意 ucfirst() 规则)?
  • 您能告诉我上面代码中的第 15 行是哪一行吗?此错误意味着您的模型没有获得任何记录返回。您的视图文件中的 me_name 属性是什么?
  • 或者你可能指向了错误的属性(数据库列名)

标签: php codeigniter


【解决方案1】:

试试这个:

<?php if($rows['me_photo'] == NULL): ?>
<img src="<?php echo base_url('/upload/be/employee/default-no-image.png'); ?>" class="img-responsive" title="no-photo" style="margin-bottom:10px" />
<?php else: ?>
<img src="<?php echo base_url('/upload/be/employee'.$rows['me_name']; ?>" class="img-responsive" style="margin-bottom:10px" />

这是一个数组。不反对。 -&gt; 代表对象。
您需要像这样$rows['me_photo'] 更改$rows-&gt;m_ephoto

【讨论】:

    【解决方案2】:

    修改您的查询

    public function getByID($id){
    $this->db->select('*');
    $this->db->from('master_employee me'); 
    $this->db->join('master_position mp', 'mp.mp_id=me.mp_id');
    $this->db->where('me.me_id', $id);
    $query = $this->db->get();
    return $query->result();
    }
    

    在视图中这样使用

    foreach ($query->result() as $row){
        echo $row->title;
     }
    

    read here i hope you will get the answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多