Nested comment and reply process using php and ajax .


CREATE TABLE `comment_reply_tbl` (
`comment_id` int(11) NOT NULL,
`parent_comment_id` int(11) NOT NULL,
`comments` varchar(200) NOT NULL,
`date` datetime NOT NULL
)

class_crud.php

<?php
include"database.php";

class Class_crud extends dbmodel{

public function insert($tablename,$data_item){
    if(!empty($tablename) && !empty($data_item) && is_array($data_item)) {
        $colname = implode(",",array_keys($data_item));
        $colvalue = implode("','",array_values($data_item));
        $sql="INSERT INTO ".$tablename."(".$colname.") VALUES
        ('".$colvalue."')";
        //echo $sql;exit;
        $q = $this->db->prepare($sql);
        $insert = $q->execute();
            if(!empty($insert)) {
                return TRUE;
            }else{
                return FALSE;
            }
    } else {
         return false;
    }
}

public function get_data($table , $con , $col){
    $sql = "SELECT ".$col." FROM ".$table." WHERE ".$con;
    $q = $this->db->prepare($sql);
    $stmt = $q->execute();
    $all_comment = $q->fetchAll();
        if( count($all_comment) > 0 ){
            return $all_comment;
        }
        else{
            return FALSE;
        }
}

public function get_reply_data($table , $con , $col){
    $sql = "SELECT ".$col." FROM ".$table." WHERE ".$con;
    $q = $this->db->prepare($sql);
    $stmt = $q->execute();
    $all_comment = $q->fetchAll();
    $count = $q->rowCount();
        if($count > 0 ){
        return $all_comment;
        }
        else{
        return FALSE;
        }
    }
}
?>
$(document).ready(function(e) {
$(document).on('click','#comment_btn',function(){
var comment = $('#comment_blog').val();
var parent_id = $('#parent_id').val();
$.ajax({
url: "comment.php",
type: "GET",
data: {comment:comment , parent_id:parent_id},
success: function(response){
location.reload();
//$('#all_commet').load(document.URL + ' #all_commet');
//alert(response);
//$("#state").html(res);
}
});
});
});
$(document).ready(function(e) {
$("#get_comment").load("get_comment.php", function(responseTxt, statusTxt, xhr){
if(statusTxt == "success")
// alert("External content loaded successfully!");
if(statusTxt == "error")
alert("Error: " + xhr.status + ": " + xhr.statusText);
});
});
$(document).ready(function(e) {
$(document).on('click','.comment_reply_child',function(){
//var comment_box_id =
$(this).parent().parent().parent().parent().parent().parent().find('.commentdiv').data('commentdivid');
alert(comment_box_id);
var comment_reply_parent = this.id;
var reply_box_count = $(".message_box").length;
var html_content = '<div class="message_box">'+
'<input type="hidden" name="parent_id" id="parent_id" value="'+
comment_reply_parent +'" />'+
'<textarea style="margin-top:5px;" placeholder="What are you doing right now?"
id="comment_blog" class="form-control"></textarea>'+
'<button style="margin-top:5px;" type="button" class="btn btn-success green"
id="comment_btn" name="comment_btn"><i class="fa fa-share"></i>
Share</button>'+
'</div>';
if( reply_box_count < 1 ){
$(this).next('.reply_box').append(html_content);
}
else{
$('.message_box').remove();
$(this).next('.reply_box').append(html_content);
}
//$("#parent_id").val(comment_reply_parent);
});
});