简体   繁体   中英

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. $id == $id is always true .

So if($id == $id && $comment_row['content']=""){ will be interpreted as if (true && false) and that is always false .

Also it would be better to name the variable $beFirst , for example, as $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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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