我有这个观点
<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);
}
以及$rows的模型:
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;
}
如果我打印r($comment),它可以很好地显示我的信息,但是由于某种原因,数据可以使用$row,但不能使用$comment,我做错了什么?错误是:
严重性:通知
消息:尝试获取非对象的属性
它是这样打印的:
stdClass对象([COMMENT\u ID]=
谢谢你的帮助!
这里没有对象数组,只是一个对象,所以请删除它,然后重试
<?php foreach ($comment as $comment): ?>
和
<?php endforeach; ?>
我把模型换成了这个,并保持了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