Ribeiro
Ribeiro

Reputation: 374

Can't pass 2 arrays of data into the view - Codeigniter

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 !

Upvotes: 1

Views: 38

Answers (2)

Niklesh Raut
Niklesh Raut

Reputation: 34924

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

Upvotes: 1

Ribeiro
Ribeiro

Reputation: 374

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

Upvotes: 0

Related Questions