<ol class="rectangle-list">
<?php
include('dbconnect.php');
$result=mysqli_query($con,"SELECT tip_unitate,tip_id FROM tipunitate ORDER BY tip_id ASC");
while($row = mysqli_fetch_array($result))
{
$pid=$row['tip_id'];
$unitate=$row['tip_unitate'];
$count=mysqli_query($con,"SELECT COUNT(*) AS num FROM unitate WHERE tip_id='$pid'");
$rowss = mysqli_fetch_array($count);
$number=$rowss['num'];
echo "<li><a href=\"map.php?pid=".$pid."\">".$unitate."(".$number.")</a></li>";
}
mysqli_close($con);
?>
</ol>
这是我的代码。我需要的是为那个 OL 中的 li 分配单独的类。有8里。我已经看到很多关于类似问题的答案,但由于我的 PHP 知识不多,我无法自己完成。
最佳答案
如果您需要为每个 li
标签分配一个唯一标识符,最简单的解决方案是将 $pid
变量指定为 id
元素。所以像这样:
echo '<li id="pid' . $pid . '">...</li>';
然后您可以根据它们的唯一 ID 为这些元素指定样式。例如:
<style>
#pid1 { ... }
#pid2 { ... }
...
</style>
您只需要在多个元素之间共享样式的类。在这种情况下,您可以这样做:
// array keys should be the PIDs associated with the classes you wish to assign
// define this array outside your loop
$styles = array(
0 => 'style1',
8 => 'style8',
);
然后在你的循环中:
echo "<li class=\"{$styles[$pid]}\"><a href=\"map.php?pid={$pid}\">{$unitate}({$number})</a></li>";
关于php - 如何回显 li 的单个 css 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26388831/