无法将2个数据数组传递到视图中 - Codeigniter

时间:2016-06-11 19:46:35

标签: php codeigniter

我有观点

<div>
<?php print_r($comment); ?>
<?php foreach ($comment as $comment): ?>
<div class="jumbotron">
<div class="alert alert-success" role="alert"><?php $comment->USER_NAME; ?>
</div>
<?php echo $comment->COMMENT_TEXT ?>
</div>
<?php endforeach; ?>
</div>

我使用此控制器将信息传递给视图:

public function restaurant_template()
{
    $id = $this->input->get('id');
    $this->load->model('restaurant_model');
    $data['row'] = $this->restaurant_model->restaurant_template($id);
    $data['comment'] = $this->restaurant_model->comments_restaurant($id);
    $this->load->view('sample_navbar_view');
    $this->load->view('restaurant_template_view', $data);
}

$行的模型:

public function restaurant_template($id)
{
 $query = "SELECT r.RESTAURANT_ID, r.RESTAURANT_NAME,r.RESTAURANT_ADDRESS,
r.RESTAURANT_RESERVATIONS, 
r.RESTAURANT_WIFI, r.RESTAURANT_DELIVERY, r.RESTAURANT_MULTIBANCO,     
r.RESTAURANT_OUTDOOR_SEATING, r.RESTAURANT_POINTS, r.RESTAURANT_IMAGE,
r.RESTAURANT_LATITUDE, r.RESTAURANT_LONGITUDE 
FROM RESTAURANTS r WHERE r.RESTAURANT_ID = '".$id."'";
$result = $this->db->query($query);
$rows = $result->row();
return $rows;
}

模型为$ comment

    public function comments_restaurant($id)
{
    $query = "SELECT CR.COMMENT_ID, CR.USER_ID, CR.COMMENT_TEXT, U.USER_NAME
    FROM COMMENTS_RESTAURANT CR JOIN USERS U
    ON CR.USER_ID = U.USER_ID WHERE CR.RESTAURANT_ID = '".$id."'";

    $result = $this->db->query($query);
    $comment = $result->row();

    return $comment;
}

如果我print_r($ comment)它显示我的信息就好了,但由于某种原因,数据与$ row一起使用,但是没有使用$ comment,我做错了什么? 错误是这样的:

  

严重性:注意

     

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

打印方式如下:

  

stdClass对象([COMMENT_ID] =&gt; 1 [USER_ID] =&gt; 1 [COMMENT_TEXT] =&gt; Este restaurante recomenda-se,pois tem um optimo funcionamento ea qualidade e muito boa [USER_NAME] =&gt; Filipa)

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

这里没有obect数组只是一个对象,所以删除它,然后尝试

 <?php foreach ($comment as $comment): ?>

并且

 <?php endforeach; ?>

答案 1 :(得分:0)

我将模型更改为此并且保持了foreach,现在它就像一个魅力。

  public function comments_restaurant($id)
{
    $query = "SELECT CR.COMMENT_ID, CR.USER_ID, CR.COMMENT_TEXT, U.USER_NAME
    FROM COMMENTS_RESTAURANT CR JOIN USERS U
    ON CR.USER_ID = U.USER_ID WHERE CR.RESTAURANT_ID = '".$id."'";

    $result = $this->db->query($query);
    $comment = $result->result();

    return $comment;
}

感谢@Rishi指出我正确的方向! :d