Welcome Guest, Not a member yet? Register   Sign In
Comment system not working.
#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


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