评论显示在codeigniter的最后一个帖子中

问题描述 投票:0回答:1

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 我有类似Facebook的帖子和评论系统,但我的问题是所有评论都显示在最后一篇帖子中。请检查我的代码,我尝试了很多方法,但是如果您有任何好的解决方案,它将无法正常工作,所以请帮助我

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;
}

这是我的视图文件

查看

<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>
    $( "#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 }?>
php codeigniter
1个回答
0
投票

我认为您的代码很复杂,因为不需要在评论表中保存用户图像,名称,您只需要使用评论ID(自动递增),帖子ID,用户ID和评论即可。您会收到特定帖子的所有评论,您应该通过该帖子的ir获取。示例:-当用户单击特定帖子时,假设其帖子ID为1,则单击此链接时,您必须将带有该帖子ID的请求传递到评论表,如果此条件为true,则查询将与此评论ID匹配重定向页面以查看所有评论并显示它们。控制者查询:-$ this-> model_name-> function_name($ id);在模型中$ this-> db-> where('post_id',$ id);$ this-> db-> get('comment_table')-> result_array();

© www.soinside.com 2019 - 2024. All rights reserved.