douzhuan1432 2013-11-23 08:55
浏览 36
已采纳

如何使用php添加不同的CSS到最后的mysql查询结果

I'm retrieving 4 latest items from the database and show them in a list. My question is that how can I add a different HTML class to the last item?! for example,

<ul>
    <li class="list">number 1</li>
    <li class="list">number 2</li>
    <li class="list">number 3</li>
    <li class="list last-child">number 4</li> 
</ul>

how can I add last-child to the last item using php?

  • 写回答

3条回答 默认 最新

  • doulin2025 2013-11-23 08:56
    关注

    Instead of assigning a class using PHP, use :last-child pseudo instead

    ul.class_name li:last-child { /* This will target last li in the ul */
       /* Styles */
    }
    

    Demo

    Better assign a class to your ul element to distinguish with other ul elements in the same document.


    If you want to literally add the class to the DOM for some reason than you can use a counter variable with mysqli_num_rows (Here I assume you are atleast using mysqli_()).

    $counter = 0;
    $total_rows = mysqli_num_rows($query);
    
    while($throw_results = mysqli_fetch_array($query)) {
       $counter++; /* Increment the counter by 1 */
       if($total_rows == $counter) {
          /* Add class if total rows = counter value */
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?