问题描述
我试图隐藏与上述帖子无关的评论,问题是我有一个包含所有评论的数据库,并且它们出现在我添加的每个帖子中......我正在尝试添加一个每个评论的数据 ID 并尝试在生成器帖子中创建一个 PHP“if”,如果不匹配则不会显示,但我认为我会让自己复杂化,请帮助我:
这是一个创建评论的 PHP 函数,正如你所看到的,我添加了一个 data-POSTER 来表示帖子的 ID:
function createCommentRow($data,$utenteloggato) {
global $conn;
if ($utenteloggato == $data['userID']) {
$response = '
<div class="comment" data-postER="'.$data['postID'].'" >
<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>
<div class="userComment" >'.$data['comment'].'</div>
<div class="reply"><a href="javascript:void(0)" data-commentID="'.$data['id'].'" onclick="reply(this)">REPLY</a></div>
<div class="replies">
<a id="option1"
data-id="'.$data['id'].'"
data-option="'.$data['tipo'].'"
href="javascript:void(0)"
onclick="goDoSomething(this);">
Delete
</a> ' ;
}
else
{
$response = '
<div class="comment" data-postER"'.$data['postID'].'" >
<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>
<div class="userComment" data-postID="'.$data['postID'].'">'.$data['comment'].'</div>
<div class="reply"><a href="javascript:void(0)" data-commentID="'.$data['id'].'" onclick="reply(this)">REPLY</a></div>
<div class="replies">
' ;
}
$sql = $conn->query("SELECT replies.id,name,comment,tipo,DATE_FORMAT(replies.createdOn,'%e/%c/%Y %T') AS createdOn,userID,postID FROM replies INNER JOIN users ON replies.userID = users.id WHERE replies.commentID = '".$data['id']."' ORDER BY replies.id DESC LIMIT 1");
while($data = $sql->fetch_assoc())
$response .= createCommentRow($data,$utenteloggato);
$response .= '
</div>
</div>
';
return $response;
}
在另一个 php 中,我显示了数据库表“post”中的所有帖子,一切正常,但在这个 div 中,评论显示:
<div class="userComments" data-postID="'.$data['id'].'" > </div>
我想设置一个 IF 条件,如果评论中的 data-postER 的值与 class="userComments" 中的 data-postID 的值不大,它就不会显示。谢谢大家
解决方法
由于您正在 $response 中创建一大段 html...
如何,在 userComment 行中添加(仅作为示例,我将逐行使用)几行或一行,并在进入 userComment 行时使用 if,例如:
else
{
$response = '
<div class="comment" data-postER"'.$data['postID'].'" >
<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>
<div class="userComment" data-postID="'.$data['postID'].'">'.$data['comment'].'</div>
<div class="reply"><a href="javascript:void(0)" data-commentID="'.$data['id'].'" onclick="reply(this)">REPLY</a></div>
<div class="replies">
' ;
}
变成这样:
else {
$response .= '<div class="comment" data-postER"'.$data['postID'].'" >';
$response .= '<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>';
if($data['postID'] == $data['id']){
$response .= '<div class="userComment" data-postID="'.$data['postID'].'">'.$data['comment'].'</div>';
}
$response .= '<div class="reply"><a href="javascript:void(0)" data-commentID="'.$data['id'].'" onclick="reply(this)">REPLY</a></div>';
$response .= '<div class="replies">';
}
这个和现在你要么创建了 userComments 要么没有...
你当然可以一次插入不止一行,但我想说明一下...
此外,我建议不要在变量中构建整个 html...而是构建 html 并将变量插入其中,如下所示:
?><div class="comment" data-postER="<?php echo $data['postID'];?>" >
这样你就可以在一个健康的 html 中嵌入 php 变量......:)