gpt4 book ai didi

php - 如何使用 php 将不同的 css 添加到最后一个 mysql 查询结果

转载 作者:行者123 更新时间:2023-12-01 00:47:27 25 4
gpt4 key购买 nike

我正在从数据库中检索 4 个最新元素并将它们显示在列表中。我的问题是如何向最后一项添加不同的 HTML 类?!例如,

<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>

如何使用 php 将 last-child 添加到最后一个元素?

最佳答案

不使用 PHP 分配类,而是使用 :last-child

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

Demo

最好为您的 ul 元素分配一个 class 以区别于同一文档中的其他 ul 元素。


如果你出于某种原因想要将 class 添加到 DOM 中,你可以使用 mysqli_num_rows 的计数器变量(这里我假设你至少使用 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 */
}
}

关于php - 如何使用 php 将不同的 css 添加到最后一个 mysql 查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20160448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com