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


Messages In This Thread
Comment system not working. - by kirasiris - 01-14-2018, 11:56 PM
RE: Comment system not working. - by neuron - 01-15-2018, 02:35 AM
RE: Comment system not working. - by kirasiris - 01-15-2018, 01:19 PM
RE: Comment system not working. - by neuron - 01-16-2018, 12:43 AM
RE: Comment system not working. - by kirasiris - 01-18-2018, 06:34 PM
RE: Comment system not working. - by InsiteFX - 01-15-2018, 05:04 AM



Theme © iAndrew 2016 - Forum software by © MyBB