Welcome Guest, Not a member yet? Register   Sign In
How to implement nested comments 2 levels deep
#1

[eluser]Andy78[/eluser]
I am trying to implement a commenting system on my website that is only two levels deep for example you will have the main comment and replies to that comment but it does not go any further:

Code:
main comment 1
   (sub_comment1)
   (sub_comment2)

main comment 2
   (sub_comment1)
   (sub_comment2)
   (sub_comment2)

etc...

Make sense?

I am creating the site in codeigniter but i think a basic php solution will do.

each row in my database table has an id and a parent_id, if the parent id is 0 then its a main comment and if its a sub-comment it will have the id of its parent comment in parent_id.

how do I feed a two dimensional array with the parent and child comments in the right order.

My current code is like so: The controller:

Code:
function status_comments($id){

    $this->load->model('status_model');//load the status model
    $this->load->model('comment_model');//load the comment model

    $status = $this->status_model->get_entry_and_category($id);
    $comments = $this->comment_model->get_comments($id);  

    if($status !== false) {

        if($comments !== false) {

            foreach($comments as $comment){

                  if($comment->reply_id == 0){

                    $comment =  

                  }
            }


            $content_data['comments'] = $comments;

        }        

        $content_data['status'] = $status;
        $data['content'] = $this->load->view('status_view', $content_data, TRUE);
        $data['title'] = $status->title.' - High Value Status';
        $data['page_title'] = $status->title;//The page H1 tag
        $this->load->view('home', $data);  

    }
The model function:
Code:
//Gets comments associated with an individual status  
function get_comments($status_id, $offset=null, $limit=null)
{
    $this->db->select('id, comment, nickname, created, reply_id');
    $this->db->from('comments');
    $this->db->where('active', 1);
    $this->db->where('status_id', $status_id);

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

    if ($query->num_rows() > 0) {

        return $query->result();      
    }

    return false;  
}
#2

[eluser]Otemu[/eluser]
Hi,

The two common methods is to use either nested set or adjacency list check out this excellent article here which goes in to quite a lot of detail about it http://mikehillyer.com/articles/managing...-in-mysql/

Also check out this post here which provides a number of solutions http://stackoverflow.com/questions/47779...-hierarchy

Hope that helps




Theme © iAndrew 2016 - Forum software by © MyBB