How to queue nested replies below comments on a comment page
Need some assistance to get around this please guys.
I’m building a nested comment page on my website with the comments/replies stored in an sql table with colums id, name, title, body, parent_id and time. Parent comments are to have parent_id of 0 while replies to the comments have a parent_id of the id of the comments been replied to. That works perfectly well.
Now I have a php page that echoes the output of the sql select statement of these columns as json encoded data :
- $stmt=$conn->prepare("SELECT * FROM comments ORDER BY created_at DESC");
- $stmt->execute();
- $result = $stmt->get_result();
- $num_of_rows = $result->num_rows;
- if ($num_of_rows > 0){
- $dataArray = array();
- while ($row = $result->fetch_assoc()){
- $data['id'] = $row['id'];
- $data['name'] = $row['name'];
- $data['title'] = $row['title'];
- $data['body'] = $row['body'];
- $data['parent_id'] = $row['parent_id'];
- $data['time'] = strtotime($row['created_at']);
- $data['time'] = date('d M, H:i a', $data['time']);
- $dataArray[] = $data;
- }
- echo json_encode($dataArray);
- }
- ?>
Now in my script, I parsed the json fetched from the fetch_comments.php file into a dynamically generated html depending on if it’s a comment (with parent_id = 0) or a reply to a comment like so:
- function getComments(){
- $.getJSON("fetch_comments.php", function(data){
- $.each(data, function(i, user){
- if (user.parent_id == 0){//if its a comment
- var comment = '<div class="row" id="media" data-id='+user.id+'>\
- <div class="col-md-1" id="media-left">\
- <a href="#">\
- <img src="images/user.png" alt="image" width="50px" height="50px" >\
- </a>\
- </div>\
- <div class="col-md-11" id="media-body">\
- <h4 class="media-heading">'+user.title+'</h4>\
- <h4><strong>'+user.name+'</strong></h4> @ '+user.time+'</h4>\
- <div class="media-text">\
- <p>'+user.body+'</p>\
- </div>\
- <div class="rep_lnk">\
- <a class="reply_link"><strong>Reply</strong></a>\
- </div><br/>\
- <div class="row">\
- <div class="col-md-1"></div>\
- <div class="col-md-10">\
- <div class="reply_form_wrapper"></div><br/><br/>\
- <div class="reply_queue"></div>\
- </div>\
- </div>\
- </div>';
- $(".media-wrapper").append(comment);//.media-wrapper is an empty main container class on the html page
-
- }else{//if its a reply
- var par_id = user.parent_id;
- var parent_cont = $(".media-wrapper").find("#media[data-id='+par_id+']");
- var replies = '<div class="media_left">\
- <a href="#">\
- <img src="images/user.png" alt="image" width="50px" height="50px" >\
- </a>\
- </div>\
- <div class="reply_media_body">\
- <h4>'+par_id+'</h4><h4>'+user.id+'</h4>\
- <h4 class="reply_media_heading"><strong>'+user.name+'</strong> @ '+user.time+'</h4>\
- <div class="reply_media_text">\
- <p>'+user.body+'</p>\
- </div>\
- </div>';
- parent_cont.find(".reply_queue").append(replies);
- }
- });
- });
- }
Now my challenge is that my replies are not appending to the .reply_queue div. Rather, few of them are displayed in the media-wrapper div like normal comments.
I can't just figure what i'm doing wrong. Any assistance will be appreciated please.