Welcome Guest, Not a member yet? Register   Sign In
Comment system not working.
#1

Hello I'm working on a comment box system in which the current logged in user can comment on any desired post.

So far I have not faced a problem in adding comments but what I'm having troubles with is in the time in which I need to display the comment of each individual post. To make it more clear, all the comments found in the DB are in all posts as well not just in their corresponding post.


This is my show method on my Post controller :

As you can see I'm calling a method in the Post_model(later on) with the name of get_post_comments . Unfortunately its not working.

Post_controller:
PHP Code:
   public function show($slug)
 
   {
        
// Get Pages by Slug
        
$data['posts'] = $this->Post_model->get_by_slug($slug);
        
        
// Get Comments per Post
        
$data['comments'] = $this->Post_model->get_post_comments($slug);
        
        
// If empty show a 404 error
        
if(empty($data['posts'])){
            
show_404();    
        }

 
       // Load template
 
       $this->template->load('public''default''posts/show'$data);
 
   

This is my method on my Post_model:
Can somebody tell me if there is a mistake?
PHP Code:
// Comments
    
public function get_post_comments($slug){
        
$this->db->select('*');
        
$this->db->from('comments');
        
$this->db->where('comments', array('slug' => $slug));
        
$this->db->join('comments''comments.id = posts.id');
        
        if(
$query->num_rows() >= 1){
            return 
$query->result();
        } else {
            return 
false;
        }
    } 


This is my view in which I'm using it(using ajax):

Code:
       <!-- Form -->
       <h4>Add a comment</h4>
       <?php echo validation_errors('<p class="alert alert-danger">'); ?>
       <?php
            $data = array(
                'id'        => 'form_comment',
                'action'    => 'public/comments/add_post_comment',
            );
        ?>
       <?php echo form_open($data); ?>
           <!-- Comments Body -->
           <div class="form-group">
               <?php echo form_label('Body', 'body'); ?>
               <?php
                   $data = array(
                       'name'          => 'body',
                       'id'            => 'body',
                       'class'         => 'form-control',
                       'value'            => set_value('body')
                   );
               ?>
               <?php echo form_textarea($data); ?>
           </div>
       <?php echo form_submit('mysubmit', 'Add Comment', array('class' => 'btn btn-primary')); ?>
       <?php echo form_close(); ?>
       <?php endif; ?>
    </section>
</div>
<script>
 $( "#id" ).submit(function() {
         $.ajax({
                type:'POST',
                url:'<?php echo base_url("index.php/public/comments/add_post_comment"); ?>',
                data:{'data':data, 'id':id},
                success:function(data){
                    $('#well').html(data);
                }
            });
    });
</script>
I do Front-End development most of the time 
Reply
#2

Can you share you comments and posts table structure;

Also there are mistakes in your get comments query:
Instead of 
       $this->db->select('*');
        
$this->db->from('comments');
        
$this->db->where('comments', array('slug' => $slug));
        
$this->db->join('comments''comments.id = posts.id');
change to 
        $this->db->select('*');
        
$this->db->from('comments');

        $this->db->join('comments''comments.id = posts.id');
        
$this->db->where(array('slug' => $slug));

        $query = $this->db->get();

where clause comes after join;

and what is "posts.id" in your query?
        
Reply
#3

Also if you do not need all the fields from the table do not select with *
select using the fields you need from the table.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(01-15-2018, 02:35 AM)neuron Wrote: Can you share you comments and posts table structure;

Also there are mistakes in your get comments query:
Instead of 
       $this->db->select('*');
        
$this->db->from('comments');
        
$this->db->where('comments', array('slug' => $slug));
        
$this->db->join('comments''comments.id = posts.id');
change to 
        $this->db->select('*');
        
$this->db->from('comments');

        $this->db->join('comments''comments.id = posts.id');
        
$this->db->where(array('slug' => $slug));

        $query = $this->db->get();

where clause comes after join;

and what is "posts.id" in your query?
        

This is my POSTS table in DB:
[Image: 1.png]

This is my COMMENTS table in database:
[Image: 2.png]

and this is my full code to "get" the comments for each post(Post_model).

PHP Code:
// Comments
 
   public function get_post_comments($slug){
 
       $this->db->select('username,email,website,body');
 
       $this->db->from('comments');
 
       $this->db->join('comments''comments.id = posts.id');
 
       $this->db->where(array('slug' => $slug));
 
       $query $this->db->get();
 
       
        if
($query->num_rows() >= 1){
 
           return $query->result();
 
       } else {
 
           return false;
 
       }
 
   
I do Front-End development most of the time 
Reply
#5

In your comments table you should add column for post_id, in that column you need to indicate which comment is related to which post, and if you there can be comment of comment you need to add parent_id column to comments table
Reply
#6

(01-16-2018, 12:43 AM)neuron Wrote: In your comments table you should add column for post_id, in that column you need to indicate which comment is related to which post, and if you there can be comment of comment you need to add parent_id column to comments table

I'm actually edited my code as you said(not exactly but it should work) but so far now it gives me an error of :


Code:
An uncaught Exception was encountered
Type: Error
Message: Cannot use object of type stdClass as array
Filename: C:\xampp\htdocs\codeigniter\application\controllers\Posts.php
Line Number: 54
Backtrace:
File: C:\xampp\htdocs\codeigniter\index.php
Line: 315
Function: require_once

Look this is my new method show :

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']; // 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);
 
   

Here is the method I'm doing to get the comments according to the post I'm currently reading or visiting:.

PHP Code:
    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 my new Comments_model:

PHP Code:
   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(base_url() . 'index.php/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$post_id);

 
           // Activity Array
 
           $data = array(
 
               'resource_id' => $this->db->insert_id(),
 
               'type'        => 'post category',
 
               'action'      => 'added',
 
               'user_id'      => $this->session->userdata('user_id'),
 
               'message'     => '('.$data['username'].') posted (' $data["body"] . ')'
 
           );

 
           // Insert Activity
 
           $this->Activity_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(base_url() . 'index.php/posts/show'.$post_id);

 
       }
 
   

This is my view in which I called the add_post_comment method in the specific ID of the post.:

Code:
       <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'    => '[email protected]',
                '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; ?>

I'ts not working . As you said before I added a post_id in my comments table in the DB but so far no results. The post table remains the same.

[Image: Capture.png]

Thanks for helping.
I do Front-End development most of the time 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB