Q:

Comments show in last post in codeigniter

Check my image all comment show in one post i am pass post_id 53 56 here is 53 comment and 56 comment you see but it show 53 comment in 56 post i have post and comments system like facebook but my problem is that all comments show in last post. please check it my code i have try many method but it's not working if you have any good solution soo please help me 
Controller
public function commentlist(){
    $conn = mysqli_connect("localhost","root","","finance");
    $memberId = 1;
    $sql = "SELECT * FROM tbl_comment INNER JOIN signup ON tbl_comment.u_id = signup.id";
    $result = mysqli_query($conn, $sql);
    $record_set = array();
    while ($row = mysqli_fetch_assoc($result)) {
        array_push($record_set, $row);
    }
    mysqli_free_result($result);
    mysqli_close($conn);
    echo json_encode($record_set);
}
public function commentadd(){
    $conn = mysqli_connect("localhost","root","","finance");
    $u_id =  $this->session->userdata('log_in')['id'];
    $u_image =  $this->session->userdata('log_in')['image'];
    $commentId = isset($_POST['comment_id']) ? $_POST['comment_id'] : "";
    $comment = isset($_POST['comment']) ? $_POST['comment'] : "";
    $postID = isset($_POST['tmp']) ? $_POST['tmp'] : "";
    $name = array();
    $name[0] = $this->session->userdata('log_in')['f_name'];
    $name[1] = $this->session->userdata('log_in')['l_name'];
    $commentSenderName = implode(" ", $name);
    $sql = "INSERT INTO tbl_comment(post_id, parent_comment_id,u_id,u_image,comment,comment_sender_name,date) VALUES ('" . $postID . "','" . $commentId . "','" . $u_id . "','" . $u_image . "','" . $comment . "','" . $commentSenderName . "','" . $date . "')";
    $result = mysqli_query($conn, $sql);
    if (! $result) {
        $result = mysqli_error($conn);
    }
    echo $result;
}

this is my view file
View
<form id="frm-comment_<?php echo $id;?>">
    <div class="input-row_<?php echo $id;?>">
        <?php if($this->session->userdata('log_in')['image'] !=NULL ): ?>
            <img src="<?php echo base_url(); ?>uploads/<?php echo $this->session->userdata('log_in')['image'];?>" style="height:42px; width:42px;" class="img-circle">
        <?php else : ?><img src="<?php echo base_url('assets/images/generic-avatar.jpg'); ?>" style="height:42px; width:42px;" class="img-circle">
        <?php endif; ?>
        <input type="hidden" name="comment_id" id="commentId_<?php echo $id; ?>"/>
        <input type="hidden" name="tmp" id="tmp" value="<?php echo $id;?>" />
        <input class="input-field" type="text" name="comment" id="comment_<?php echo $id; ?>" placeholder="Write a Comment..">
    </div>
    <input type="button" class="btn-submit" id="submitButton_<?php echo $id;?>" value="Comment" >
    <div id="comment-message_<?php echo $id;?>" style='display: none;'>Comments Added Successfully!</div>
    <div class="output_<?php echo $id?>" ></div>
</form>
</div>
</div>
</div>
</div>

Script 
<script>
    $( "#com_<?php echo $id; ?>" ).click(function() {
        $("#comment_<?php echo $id;?>").focus();
    });

    function postReply(commentId) {
        $('#commentId_<?php echo $id; ?>').val(commentId);
        $("#comment_<?php echo $id; ?>").focus();
    }

    $(document).ready(function () {
        listComment();
    });

    $("#submitButton_<?php echo $id;?>").click(function () {
        $("#comment-message_<?php echo $id;?>").css('display', 'none');
        var str = $("#frm-comment_<?php echo $id;?>").serialize();

        $.ajax({
            url: "<?php echo base_url();?>finance/commentadd",
            data: str,
            type: 'post',
            success: function (response)
            {
                var result = eval('(' + response + ')');
                if (response)
                {
                    $("#comment-message_<?php echo $id;?>").css('display', 'inline-block');
                    $("#comment_<?php echo $id; ?>").val("");
                    $("#commentId_<?php echo $id; ?>").val("");
                    listComment();
                } else
                {
                    alert("Failed to add comments !");
                    return false;
                }
            }
        });
    });

    function listComment(){
        $.post("<?php echo base_url();?>finance/commentlist",
            function (data) {
                var data = JSON.parse(data);
                var comments = "";
                var replies = "";
                var item = "";
                var parent = -1;
                var results = new Array();
                var list = $("<ul class='outer-comment'>");
                var item = $("<li>").html(comments);

                for (var i = 0; (i < data.length); i++)
                {
                    var commentId = data[i]['comment_id'];
                    var post_id = data[i]['post_id'];
                    parent = data[i]['parent_comment_id'];
                    if (parent == "0")
                    {comments = " \ <div class='comment-row'><div class='comment-info'>\ <img src='<?php echo base_url(); ?>uploads/"+data[i]['image']+"' class='img-circle' style='height:47px; width:47px; border:1px solid white;'>\<div class='comment-text'>\ <span>" + data[i]['post_id'] + "</span><span class='posted-by'>" + data[i]['comment_sender_name'] + "</span> &nbsp;&nbsp;&nbsp;  " + data[i]['comment'] + "</div>\
    </div>\
     <div>\
    <a class='btn-reply' style='font-size:14px;' onClick='postReply(" + commentId + ")'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reply</a>\
     &nbsp;&nbsp;&nbsp;<span class='posted-at' style='font-size:12px;'>" + data[i]['date'] + "</span>\
     </div>\
    </div>";
                        var item = $("<li>").html(comments);
                        list.append(item);
                        var reply_list = $('<ul>');
                        item.append(reply_list);
                        listReplies(commentId, data, reply_list);
                    }
                }
                $(".output_<?php echo $id?>").html(list);
            });
    }
    function listReplies(commentId, data, list) {
        for (var i = 0; (i < data.length); i++)
        {

            if (commentId == data[i].parent_comment_id)
            {
                var comments = "\ <div class='comment-row'><div class='comment-info'>\ <img src='<?php echo base_url(); ?>uploads/"+data[i]['image']+"' class='img-circle' style='height:47px; width:47px; border:1px solid white;'>\
     <div class='comment-text'> <span class='posted-by'>" + data[i]['comment_sender_name'] + "</span> &nbsp;&nbsp;&nbsp; " + data[i]['comment'] + "</div>\
     </div>\
    <div>\
                                             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\ <span class='posted-at' style='font-size:12px;'>" + data[i]['date'] + "</span>\
     </div>\
    </div>";
                var item = $("<li>").html(comments);
                var reply_list = $('<ul>');
                list.append(item);
                item.append(reply_list);
                listReplies(data[i].comment_id, data, reply_list);
            }
        }
    }
</script>
<?php }?>

A:

I think your code is complex because there is not need to save user image ,name in comment table ,you need to use only comment id(auto increment) post id, user id , comment.
 Yo get all comments for particular post you should fetch by post ir for that post.
Example :- when user click on particular post , suppose its post id is 1 then as click click on this you have to pass a request with this post id to comment table where query will match this post id in comment id if this condition true then redirect page to view with all comment and show them.
Controller
Query:- $this->model_name->function_name($id); 
In model
$this->db->where('post_id', $id);
$this->db->get('comment_table')->result_array();
This query will fetch all comment for clicked post.

