douchunji1885 2019-01-07 00:45
浏览 61
已采纳

如何在自动添加的div中添加不同的数字?

Im working on a confession website, and on the website I have a section "Top of the Week" which displays 3 most liked confessions from current week, it displays them in a horizontal slider by most liked order, so it goes most liked, second most liked, third most liked confession. The thing that Im trying to do is to put #1, #2, #3 number on each one, but I can't achieve that because I only style one div and website automatically create two other and add confession to it.

This is my php + html code that I use to display them:

<div class="sidebox">

<?php
$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 limit 3";
                $resss = mysqli_query($mysqli, $select) or die('-3' . mysqli_error());  ?>

            <div id="sticky-nav" style="height:36px;" class="absolute" style="z-index:0">
        <div id="width-limit">
            <div class="options">
                <ul class="menu">
                            <li><a class="carousel_prev previous" href="#"><b style="font-size: 20px;">&#8249;</b></a></li>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<li><a style="opacity:1;padding-top:1px;position:relative;left:-54px;"><i class="fas fa-crown" id="crownicon"></i> Top of the Week</a>&nbsp;</li> 
                            <li><div class="menu-clear"></div></li> 
                            <li><a class="carousel_next next" href="#"><b style="font-size: 20px;">&#8250;</b></a></li>         
                        </ul>
            </div>
            <div class="options" id="opt2" style="float:right; width:280px;display:none;">
                <ul style="display: inline-block;float:right;">

                </ul>
            </div><div class="options" id="opt2" style="float:right; width:280px;display:none;">
                <ul style="display: inline-block;float:right;"> 
                </ul>
            </div>

            <div id="small-logo"></div>
            <div class="clearfix"></div>
        </div>
        </div><div class="sidecontainer">
<div class="slick">
<?php
                        while ($row = mysqli_fetch_assoc($resss)) {
                            // 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" style="margin-left: 0;width: 300px;">
                                <div class="left"><span class="label2 label-confess1"><?php echo $row['totalViews'].' '.$viewText; ?></span></div>
                              <div class="right"><span class="bestthisweek">

                              <?php if ($row['totalLikes'] == '12') { echo "Top of the Day!"; } else { echo "Top!"; } ?>

                              </span></div>
                                <div class="confessionstyle" style="margin-top:20px;"><p> 
                                    <font color="#fff3b2">
                                    <?php
                                        if ($filterProfanity == '1') {
                                            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']; ?>" />
                                <?php
                                    $chkLikes = mysqli_query($mysqli,"SELECT 'X' FROM likes WHERE confessId = ".$row['confessId']." AND likeIp = '".$_SERVER['REMOTE_ADDR']."' LIMIT 1");
                                    $hasLike = mysqli_num_rows($chkLikes);

                                    $likeCSS = $hasLike > 0 ? 'text-info' : 'white';

                                    $chkDislikes = mysqli_query($mysqli,"SELECT 'X' FROM dislikes WHERE confessId = ".$row['confessId']." AND dislikeIp = '".$_SERVER['REMOTE_ADDR']."' LIMIT 1");
                                    $hasDislike = mysqli_num_rows($chkDislikes);

                                    $dislikeCSS = $hasDislike > 0 ? 'text-danger' : 'white';
                                ?>
                                <div class="confession-actions">                                    
                                    <div class="likes" style="width: 75px;">
                                        <span class="label2 label-confess first liked">                                         
                                            <a href="" id="likeIt_<?php echo $row['confessId']; ?>" class="likeIt_<?php echo $count; ?> <?php echo $likeCSS; ?>" style="text-decoration:none;outline:none;">
                                                <i class="fas fa-thumbs-up"></i> <span style="color:white;" id="likesVal_<?php echo $row['confessId']; ?>"><?php echo $row['totalLikes']; ?></span>
                                            </a>
                                        </span>
                                        </div>
                                    <div class="dislikes" style="width: 75px;">
                                        <span class="label2 label-confess disliked">
                                            <a href="" id="dislikeIt_<?php echo $row['confessId']; ?>" class="dislike_<?php echo $count; ?> <?php echo $dislikeCSS; ?>" style="text-decoration:none;outline:none;">
                                                <span style="color:white;" id="dislikesVal_<?php echo $row['confessId']; ?>"><?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" style="width: 75px;"><div id="comments-hvr"><a href="page.php?page=view&confession=<?php echo $row['confessId']; ?>">
                                            <i class="fa fa-comments"></i> <?php echo $totComments.' '; ?>
                                            </a></div></div>
                                    </div>

                                <div class="divide2" style="width: 75px;"><div class="fb-share-button" style="top:-6.5px;transform: scale(0.93);" 
    data-href="page.php?page=view&confession=<?php echo $row['confessId']; ?>" 
    data-layout="button_count"></div></div></div>

                                <div class="clearfix"></div>
                            </div>
                    <?php

                        }
                    ?>
               </div>

I tried everything and this is the best solution that I have, but I can't figure out what should I put inside if, elseif, else, I need something that will place #1 on first, #2 on second, #3 on third confession. So I would need something like this but something that will not require me to manually insert > likes.

<?php if ($row['totalLikes'] > '11') { echo "#1"; } elseif ($row['totalLikes'] > '5') { echo "#2"; } else { echo "#3"; }?>

Please visit my website, its still in development, but check Top of the Week on the right and you'll have a clue of what I actually want: http://confessions.byethost31.com

FINAL:

<?php $i = 1; ?>

    <?php

echo '#'.$i;
    $i++;

?>
  • 写回答

1条回答 默认 最新

  • douzhan4522 2019-01-07 00:56
    关注

    Easiest way to to just create a variable that increments with every while iteration.

    $i = 1; // set initial value     
    while ($row = mysqli_fetch_assoc($resss)) {
        echo 'This number will grow by 1 for every row - '.$i;
        $i++; // this is equal to $i = $i + 1; so will increment by 1 every time.
    }
    

    EDIT:

    Instead of attempting to assume what the total likes would be, it would be easier to sort them by total likes (putting the most liked first and assigning this #1).

    ORDER BY totalLikes DESC, totalViews DESC, orderDate DESC

    You can then just use

    echo '#'.$i;
    

    Where you want to display the number, you can do this because you have LIMIT 3 so only the top 3 will display.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题