1

I have ths view

<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>

I'm passing the information to the view with this controller:

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);
}

And the Model for $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;
}

And Model for $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;
}

If i print_r($comment) it shows my information just fine, but for some reason the data works with $row, but doesn't work with $comment, what am i doing wrong? The error is this:

Severity: Notice

Message: Trying to get property of non-object

It prints like this:

stdClass Object ( [COMMENT_ID] => 1 [USER_ID] => 1 [COMMENT_TEXT] => Este restaurante recomenda-se, pois tem um optimo funcionamento e a qualidade e muito boa [USER_NAME] => Filipa )

Thanks for the help !

2 Answers 2

1

There is no array of obects here is just a single object, so remove this, and try

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

And

 <?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

2 Comments

It works, but what if i have 2 rows retrieved from the database?
For that dont remove as given code, just pass array of objects.
0

I changed the model to this and keeped foreach and now it works like a charm.

  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;
}

Thank you @Rishi for pointing me in the right direction ! :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.