I'm working on a comment section for my blog post in CodeIgniter, I based myself on a tutorial by Brad Traversy and I followed each step carefully but somehow it does not work within my template.
This is show method in my Post Controller:
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']; // Here is where I get the error
$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);
}
I created a variable $post_id in order to get the comments for the current post that is being visited by an user. All this should come from my Post_model:
public function get_comments($post_id){
$this->db->select('username,email,website,body');
$this->db->from('comments');
$query = $this->db->get_where('comments', array('post_id' => $post_id));
return $query->result_array();
}
This is where I'm creating the form to add the comment:
<!-- Form -->
<h4>Add a comment</h4>
<?php echo validation_errors('<p class="alert alert-danger">'); ?>
<?php echo form_open('public/comments/add_post_comment/'.$post['id']); ?>
<!-- Username -->
<div class="form-group">
<?php echo form_label('Username', 'username'); ?>
<?php
$data = array(
'id' => 'username',
'name' => 'username',
'class' => 'form-control',
'placeholder' => 'John Doe',
'value' => set_value('username')
);
?>
<?php echo form_input($data) ?>
</div>
<!-- Email -->
<div class="form-group">
<?php echo form_label('E-mail', 'email'); ?>
<?php
$data = array(
'id' => 'email',
'name' => 'email',
'class' => 'form-control',
'placeholder' => 'JohnDoe@demo.com',
'value' => set_value('email')
);
?>
<?php echo form_input($data) ?>
</div>
<!-- Website -->
<div class="form-group">
<?php echo form_label('Website', 'website'); ?>
<?php
$data = array(
'id' => 'website',
'name' => 'website',
'class' => 'form-control',
'placeholder' => 'https://www.example.com',
'value' => set_value('website')
);
?>
<?php echo form_input($data) ?>
</div>
<!-- Comments Body -->
<div class="form-group">
<?php echo form_label('Body', 'body'); ?>
<?php
$data = array(
'id' => 'body',
'name' => 'body',
'class' => 'form-control',
'placeholder' => 'Write here',
'value' => set_value('body')
);
?>
<?php echo form_textarea($data); ?>
</div>
<!-- Hidden Input -->
<?php
$data = array(
'name' => 'slug',
'value' => $posts->slug,
);
?>
<?php echo form_hidden($data); ?>
<!-- Submit Button -->
<?php echo form_submit('mysubmit', 'Add Comment', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>
<?php endif; ?>
This is the Controller I'm using to add comments to the specific $post_id:
public function add_post_comment($post_id)
{
// Field Rules
$this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[3]');
$this->form_validation->set_rules('body', 'Body', 'trim|required|min_length[3]');
if ($this->form_validation->run() == FALSE) {
// Set Message
$this->session->set_flashdata('error', 'There was an error in proccessing the comment. Please, try again.');
// Redirect to current page
redirect(site_url() . 'posts/show/'.$post_id);
} else {
// Get Post by Slug
$slug = $this->input->post('slug');
$data['posts'] = $this->Post_model->get_by_slug($slug);
// Create Post Array
$data = array(
'post_id' => $post_id,
'username' => $this->input->post('username'),
'user_id' => $this->session->userdata('user_id'),
'email' => $this->input->post('email'),
'website' => $this->input->post('website'),
'body' => $this->input->post('body'),
);
// Insert Comments
$this->Comments_model->add($data);
// Set Message
$this->session->set_flashdata('success', 'Your comment has been posted');
// Redirect to same page if form was not successful or submitted
redirect(site_url() . 'posts/show/'.$post_id);
}
}
All this should work according to some tutorials that I have seen before but this time is not.
The error comes from my function show. Any idea on how to fix this? Thanks for helping.