简体   繁体   English

如果没有发表评论,则显示“成为第一个对帖子发表评论”的消息

[英]display “be the first to comment on a post” message if no comments are made php mysqli

I have a code here but cannot seem to get it working. 我在这里有一个代码,但似乎无法正常工作。 I would like to display a message like "Be the first to comment" if no one has posted a comment on that users post. 如果没有人在该用户的帖子上发表评论,我想显示一条消息,例如“成为第一个发表评论的人”。 My code is as follows: 我的代码如下:

<?php 
                            $comment_query = mysqli_query($con,"SELECT * ,UNIX_TIMESTAMP() - date_posted AS TimeSpent FROM comment LEFT JOIN users on users.id = comment.user_id where post_id = '$id'") or die (mysqli_error());
                            while ($comment_row=mysqli_fetch_array($comment_query)){
                            $comment_id = $comment_row['comment_id'];
                            $comment_by = $comment_row['name'];
                            if($id == $id && $comment_row['content']=""){
                                $beFirst = "Be the first to comment";
                            } else {
                                $beFirst = $comment_row['content'];
                            }
                        ?>
                <br><img src='<?= $user_form_img; ?>' height=24px width=24px align=left style="display:inline-block"/><p style="padding-left:24px;"><a href="#"><?php echo $comment_by; ?></a><br/><font style=font-size:18px;><?php echo $comment_row['content']; ?><?= $beFirst; ?>
                </font><br>
                        <?php               
                            $days = floor($comment_row['TimeSpent'] / (60 * 60 * 24));
                            $remainder = $comment_row['TimeSpent'] % (60 * 60 * 24);
                            $hours = floor($remainder / (60 * 60));
                            $remainder = $remainder % (60 * 60);
                            $minutes = floor($remainder / 60);
                            $seconds = $remainder % 60;
                            if($days > 0)
                            echo date('F d, Y - H:i:sa', $comment_row['date_posted']);
                            elseif($days == 0 && $hours == 0 && $minutes == 0)
                            echo "A few seconds ago";       
                            elseif($days == 0 && $hours == 0)
                            echo $minutes.' minutes ago';
                        ?>

Any help would be greatly appreciated. 任何帮助将不胜感激。

You need to replace this check: 您需要替换此检查:

if($id == $id && $comment_row['content']=""){

with

if ($comment_row['content'] == '') {

Because using $comment_row['content']="" , you assign an empty value to variable and boolean value will be false in that case. 因为使用$comment_row['content']="" ,所以您为变量分配了一个空值,在这种情况下,布尔值将为false $id == $id is always true . $id == $id始终为true

So if($id == $id && $comment_row['content']=""){ will be interpreted as if (true && false) and that is always false . 因此, if($id == $id && $comment_row['content']=""){将被解释为if (true && false) ,并且始终为false

Also it would be better to name the variable $beFirst , for example, as $commentMsg . 同样,最好将变量$beFirst命名为$commentMsg Because the main value there will be the text of the comment and not the default text ("Be the first to comment"). 因为主要值是注释文本,而不是默认文本(“第一个注释”)。 Or just don't create the variable and use ternary operator: 或者只是不创建变量并使用三元运算符:

<?= ($comment_row['content'] == '') ? "Be the first to comment" : $comment_row['content'] ?>

Also I highly recommend to use PHP PDO prepared statements to prevent SQL injections. 另外,我强烈建议使用PHP PDO准备好的语句来防止SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM