(01-24-2018, 04:34 AM)InsiteFX Wrote: Yes, I would return an associated array instead to keep your method all the same.
I already changed the $query->row() to $query->result_array() in the get_by_slug method but it now gives me an error of undefined index which comes from the $post_id = $data['posts']->id;
Now if I use $query->row_array() instead of $result_array() it does not gives error and I can see the post clearly with not problem but just by changing the $post_id = $data['posts']->id; to $post_id = $data['posts']['id']; as well.
Here is my new methods in the controller:
PHP Code:
public function show($slug)
{
// Get Posts by Slug
$data['posts'] = $this->Post_model->get_by_slug($slug);
// Get Comments per Post
$post_id = $data['posts']['id'];
$data['comments'] = $this->Post_model->get_comments($post_id);
// If empty show a 404 error
if(empty($data['posts'])){
show_404();
}
// Load template
$this->template->load('public', 'default', 'posts/show', $data);
}
and here my two method on the Post_model:
PHP Code:
public function get_by_slug($slug)
{
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('slug', $slug);
$this->db->where('is_published', 1);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->row_array();
} else {
return false;
}
}
// Comments
public function get_comments($post_id){
$this->db->select('*');
$this->db->from('users');
$query = $this->db->get_where('comments', array('post_id' => $post_id));
if($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
is basically the same I still don't know how to show users info and avoid the duplicate of comments. I will show you some screenshot of my three tables posts,comments, users.
Post table:
Comments table:
Users table:
Not sure if I could explain myself but thanks for helping I really want to get this done!.
NOTE: The view is stil the same regarding the comment section, the only thing I had to change was the way in which to disply the post title and content from:
$posts->title; to $posts['title']; in order to make it work with the $post_id = $data['posts']['id'];
How can I avoid the duplicate of comments and how can I show the users info in the same view as I would like to add a link-to-profile in the profile picture of each comment.
I do Front-End development most of the time