如何只接受最受欢迎的帖子并将其显示在其他部分?

时间:2018-12-11 18:09:01

标签: javascript php mysql

我有一个网站,使用户无需注册或登录即可发帖,我在页面上显示大多数popualar帖子,但用户可以选择显示新的帖子(最喜欢或不喜欢的帖子,随机的帖子等)...现在我想从我最受欢迎的帖子页面中挑选第一名帖子(喜欢/不喜欢最多的帖子)。我将尽力解释。

这是我的帖子供稿,因此用户可以在热门,最佳和最新之间进行选择,并且可以看到帖子以垂直方式显示。在我的边箱中,我只想放置我的热门帖子供稿中的一篇最受欢迎的帖子。

我的帖子以及我想在其中放置最受欢迎的帖子

以下是我显示帖子的代码:

       <?php
    while ($row = mysqli_fetch_assoc($res)) {
    // Get Total Comments
    $comssql = "SELECT 'X' FROM comments WHERE confessId = ".$row['confessId']." AND isActive = 1";
    $commentstotal = mysqli_query($mysqli, $comssql) or die('-4'.mysqli_error());
    $totComments = mysqli_num_rows($commentstotal);
    if ($totComments == '1') { $comText = 'Comment'; } else { $comText = 'Comments'; }
    if ($row['totalViews'] == '1') { $viewText = 'View'; } else { $viewText = 'Views'; }
    $shareURL = $set['installUrl'].'page.php?page=view&confession='.$row['confessId'];
                            ?>
    <div class="confession">
    <div class="left"><span class="label label-confess1"><?php echo $row['totalViews'].' '.$viewText; ?></span></div>
    <div class="right"><span class="label label-confess2"><?php echo timeago($row['postDate']); ?></span></div>
    <div class="confessionstyle"><p>
    <font color="#fff3b2">
    <?php
    if ($filterProfanity == '0') {
    echo nl2br(htmlspecialchars(filterwords($row['confessText'])));
                                                } else {
                                                    echo nl2br(htmlspecialchars($row['confessText']));
                                                }
                                            ?>
                                            </font>
    </p></div>

    <input type="hidden" id="confessId" name="confessId_<?php echo $count; ?>" value="<?php echo $row['confessId']; ?>" />
 <div class="confession-actions">

  <div class="likes">
  <span class="label label-confess first liked">

     <a href="" id="likeIt<?php echo $row['confessId']; ?>" class="likeIt_<?php echo $count; ?> text-success">
                                                        <i class="fas fa-thumbs-up"></i> <span id="likesVal_<?php echo $count; ?>"><?php echo $row['totalLikes']; ?></span>
       </a>
            </span>
             </div>
      <div class="dislikes">
  <span class="label label-confess disliked">
   <a href="" id="dislikeIt<?php echo $row['confessId']; ?>" class="dislike_<?php echo $count; ?> text-danger">
    <span id="dislikesVal_<?php echo $count; ?>"><?php echo $row['totalDislikes']; ?></span> <i class="fas fa-thumbs-down"></i>
        </a>
     </span>
    </div>



          <?php if ($row['hasImage'] != '0') { ?>
         <span class="label label-confess"><i class="fa fa-picture-o img"></i></span>
                <?php } ?>

            <div class="comments">

    <div class="divide"><a href="page.php?page=view&confession=<?php echo $row['confessId']; ?>">
                                                        <i class="fa fa-comments"></i> <?php echo $totComments.' '.$comText; ?>
               </a></div>
       </div>

 <div class="divide2"><a href="https://twitter.com/intent/tweet?text=<?php echo $set['siteName']; ?>%20Confession:%20<?php echo ellipsis($row['confessText'],65); ?>%20&url=<?php echo $shareURL; ?>" class="btn btn-tw btn-sm" target="_blank" data-toggle="tooltip" data-placement="top" title="<?php echo $twitterShareTooltip; ?>">
<i class="fab fa-twitter" style="color:white"></i>
  </a></div></div>

 <div class="clearfix"></div>
</div>
<?php
$count++;
      }
?>
<a href="#" class="btn btn-loadmore btn-block" id="loadMore"><?php echo $loadMoreText; ?></a><br />
</div>
        <?php
if(mysqli_num_rows($adres) > 0) {
while ($ad = mysqli_fetch_assoc($adres)) {
                        ?>
<div class="adText">
<h3><a href="<?php echo clean($ad['adUrl']); ?>"><?php echo clean($ad['adTitle']); ?> <i class="fa fa-external-link pull-right"></i></a></h3>
<p><a href="<?php echo clean($ad['adUrl']); ?>"><?php echo nl2br(clean($ad['adText'])); ?></a></p>
<span class="label label-default"><?php echo $advertisementText; ?></span>
<div class="clearfix"></div>
</div>
<?php
}
}
?>

</div>

以下是用于显示最受欢迎帖子的php:

    else if ($view == $viewNavLinkPopular) {
                    $select = "SELECT
                confessions.confessId,
                (IFNULL(confessions.firstName, '')) AS firstName,
            confessions.confessText,
            DATE_FORMAT(confessions.postDate,'%b %d %Y %h:%i %p') AS postDate,
            hasImage,
            UNIX_TIMESTAMP(confessions.postDate) AS orderDate,
            confessions.isActive,
            (SELECT COUNT(*) FROM views WHERE views.confessId = confessions.confessId ) as totalViews,
            (SELECT COUNT(*) FROM likes WHERE likes.confessId = confessions.confessId ) as totalLikes,
            (SELECT COUNT(*) FROM dislikes WHERE dislikes.confessId = confessions.confessId ) as totalDislikes
            FROM
            confessions
            WHERE isActive = 1
            ORDER BY totalViews DESC, orderDate DESC";
            $res = mysqli_query($mysqli, $select) or die('-3' . mysqli_error());
                }

如果您需要任何其他代码来帮助我,请问。 我尝试放入用于显示它们的相同代码,但将计数更改为1,但这没有帮助。为了明确起见,我想从“热门”部分中获取最受欢迎的帖子,并在“最喜欢的自白”部分中显示它(请参见我所附的图片)。希望获得帮助,谢谢建议!

1 个答案:

答案 0 :(得分:0)

我可以给你一个建议。在查询中,您将获得最喜欢或不喜欢的过滤器结果。尝试在最后输入查询“ LIMIT 1”。如果那不起作用。尝试获取所有输出,它们仅打印输出的第一部分。 希望这会有所帮助