簡體   English   中英

控制器未調用模型函數

[英]Controller not calling model function

我正在嘗試調用函數get_owner($id)以獲取用戶名,但它一直在說:遇到PHP錯誤

Severity: Notice

Message: Trying to get property of non-object

Filename: incident/detail_page_admin.php

Line Number: 54

視圖:

<p id ="detail_owner">Criador do Incidente: <?php echo $incident_own->name;?></p>

控制器:

$aux = $this->incident_model->count_incident($this->session->userdata('usuario_id'));
$avatar = $this->usuario_model->get_avatar($this->session->userdata('usuario_id'));
$incident_owner = $this->usuario_model->get_owner($id);
$this->load->view('template/cabecalho');
$b = $this->incident_model->get_incident_by_id($id);
$this->load->view('incident/detail_page_admin', array('incident_own' => $incident_owner, 'count_incident' => $aux, 'incident_show_by_id' => $b, 'avatar_array' => $avatar));
$this->load->view('template/rodape');

模型:

function get_owner ($id){
    $a = $this->db->query('select name from usuario where id = ?', array($id));
    return $a->result();
}

而不是在模型函數中返回$a->result() ,而是要返回$a->row() ,然后它應該可以工作。

function get_owner ($id){
    $a = $this->db->query('select name from usuario where id = ?', array($id));
    return $a->row();
}

但是,返回$a->row()只會返回一行,如果只期望一行,那很好。 但是,如果您希望返回多行,則可以執行

function get_owner ($id){
    $a = $this->db->query('select name from usuario where id = ?', array($id));
    return $a;
}

然后,您想像這樣遍歷視圖中的結果行

<?php foreach ($incident_own->result() as $row)
{ ?>
    <p id ="detail_owner">Criador do Incidente: <?php echo $row->name;?></p>
<?php } ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM