在我说什么之前我会发布我的index.php,让大家先了解一下它的样子:
<script>
function submitLikeForm() {
var form = jQuery("#like_form");
$.ajax({ type: "GET", url: form.attr("action"), data: form.serialize(), success: function() });
}
</script>
<script>
function leaveYourComment() {
var form = jQuery("#comment_form");
$.ajax({ type: "GET", url: form.attr("action"), data: form.serialize(), success: function() });
}
</script>
<link type="text/css" rel="stylesheet" href="index.css">
</head>
<body>
<?php
$conn = mysqli_connect("localhost","root","") or die ("No SQLI");//run query to display all images uploaded by users
mysqli_select_db($conn, "sample") or die ("No DB");
$sqli = "SELECT * FROM `photos` ORDER BY `id` DESC";
$result = mysqli_query($conn, $sqli) or die ("No query");
while($row = mysqli_fetch_assoc($result)) {
$username = $_SESSION['id'];
$title = $row['title'];
$description = $row['description'];
$image_name = $row['image_name'];
$random_directory = $row['random_direc'];
$date = date('M/d/Y');
$image_info = "http://localhost/splindr_2.0/photos/$random_directory/$image_name";
$sqli = ("SELECT * FROM `likes` WHERE `hidden_name` = '$image_name' AND `hidden_folder` = '$random_directory'");//run query to get likes
$likes_result = mysqli_query($conn, $sqli) or die ("No query");
$liked_count = '0' ; $unliked_count = '0';
while($likes_row = mysqli_fetch_assoc($likes_result)) {
if($likes_row['liked_unliked'] == 0) {
$unliked_count++;
} else {
$liked_count++;
}
}
$comment_count = ("SELECT * FROM `comments` WHERE `hidden_folder` = '$random_directory' AND `hidden_name` = '$image_name'");//run query to get comments
$comment_query = mysqli_query($conn, $comment_count) or die ("No query");
$total_comments = mysqli_num_rows($comment_query);
while($comment_result = mysqli_fetch_assoc($comment_query)) {
$comment = $comment_result['comment'];
$commented_on_by = $comment_result['commenting_user'];
}
echo "<div id='contentWrapper'>
<div class='photo'>
<div class='actual_image'>
<img src='$image_info'>
</div>
<div class='like_system'>
<form name='like_form' id='like_form' action='liked_button.php'>
<input type='image' img src='https://i.imgur.com/pMmYaYM.jpg' class='like_button button' name='like_button' onClick='return submitLikeForm()'>
<input type='hidden' name='hidden_image' class='hidden_image' value='$image_info'>
<input type='hidden' name='hidden_folder' class='hidden_folder' value='$random_directory'>
<input type='hidden' name='hidden_name' class='hidden_name' value='$image_name'>
<input disabled type='text' name='total_likes' class='total_likes' value='$liked_count'>
</form>
<a style='color: #2e4987;text-decoration:none' href='http://www.twitter.com/share?url=$image_info&hashtags=Splindr, CheckThisOut'>
<img class='twitter_button button' name='twitter_button' src='http://i.imgur.com/hKFKJoj.jpg'></a>
<img src='http://i.imgur.com/8azoFjh.jpg' class='show_comments button' name='show_comments'>
<input disabled type='text' class='comment_count' name='comment_count' value='$total_comments'>
<img class='delete_button button' name='delete_button' src='http://i.imgur.com/wKZu2H1.jpg'>
</div>
<div class='info_wrapper'>
<div class='info_header'>Title: $title   By: $username   Date: $date</div>
<div class='description'>$description</div>
</div>
<div class='comment_wrapper'>
<div class='close_comment'>[close comments]</div>
<div class='comment_box_wrapper'>
<div class='comment_box'>
<div class='commenter'>$commented_on_by</div>
<div class='profile_pic'><img src='https://i.imgur.com/1MkFvhU.png'></div>
<div class='actual_comment custom_scrollbar'>$comment</div>
</div>
</div>
<div class='post_comment'>
    <a style='color:#9b6bb4 ; text-decoration:none ; position:relative ; top:-5px ; left:-5px'></a>
<form name='comment_form' id='comment_form' action='leave_comments.php' onSubmit='return validateForm()'>
<input type='text' name='user_comment' class='user_comment' placeholder='Leave a comment...'>
<input type='hidden' name='hidden_image' class='hidden_image' value='$image_info'>
<input type='hidden' name='hidden_folder' class='hidden_folder' value='$random_directory'>
<input type='hidden' name='hidden_name' class='hidden_name' value='$image_name'>
<input type='image' img src='http://i.imgur.com/8azoFjh.jpg' class='leave_comment button' name='leave_comment button' onClick='return leaveYourComment ()'>
</form>
</div>
</div>
</div>
</div>";
}
?>
好的,所以基本上,所以这个index.php页面是用户上传后的图片。用户可以评论并喜欢帖子/图片。
我遇到的问题是我的评论系统。评论上传到mysqli很好,但是,我很难回应每个评论。我的表格中显示了多条评论,但是当我发布新评论时,新评论只会替换之前发布的评论。我不想要这个,我希望每个评论都显示在id DESC
。
我已经为评论尝试了一个循环(正如您在上面的代码中看到的那样),但就像我说的那样,它只会使最新的评论取代旧的评论。
那么我该如何更正我的代码,以便所有评论都显示在comment_box
中的comment_box_wrapper
内,以及发布评论的用户的相应信息。
我整天都在挣扎,所以任何帮助都会受到赞赏。
如果您需要我发布任何其他内容,请告诉我。我只发布了index.php,因为其他一切正常,问题只出现在这个页面上。
答案 0 :(得分:2)
您在循环中分配$ comment和$ commented_by变量,有效地覆盖它们,而不使用它们。
您需要将一个大echo
拆分,并将comment_box部分替换为以下内容:
while($comment_result = mysqli_fetch_assoc($comment_query)) {
echo "<div class='comment_box'>";
echo "<div class='commenter'>".$comment_result['commenting_user']."</div>";
echo "<div class='profile_pic'><img src='https://i.imgur.com/1MkFvhU.png'></div>";
echo "<div class='actual_comment custom_scrollbar'>".$comment_result['comment']."</div>";
echo "</div>";
}
如上所述,我们在循环的每次迭代中打印每个评论数据。