我想知道是否有可能以一种简单的方式,当我使用 while 函数调出一堆数据并将它们放入表中时,但我想使用 CSS 使其成为具有常规背景的一行和具有常规背景的一行浅色背景。
希望我说的有道理......
这是我的:
echo "<style type='text/css'>";
echo "th {background: #CEED5E;}";
echo "</style>";
echo "<table border=1>";
echo "<tr><th>Product ID</th><th>Title</th><th>Author</th><th>Description</th></tr>";
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>" . $row["id"] . "</td><td>" . $row["title"] . "</td><td>" . ucwords(strtolower($row["authorFirstName"] . " " . $row["authorLastName"])) . "</td><td>" . $row["description"] . "</td></tr>";
}
echo "</table>";
echo "<style type='text/css'>";
echo ".normalbackground {background-color: white;}";
echo ".coloredbackground {background-color: #CEED5E;}";
echo "</style>";
echo "<table border=1>";
echo "<tr><th>Product ID</th><th>Title</th><th>Author</th><th>Description</th></tr>";
$i = 0;
while ($row = mysqli_fetch_assoc($result))
{
$i++; if (($i % 2) == 0) {$class = "coloredbackground";} else {$class = "normalbackground";};
echo "<tr><td class="$class">" . $row["id"] . "</td><td class="$class">" . $row["title"] . "</td><td class="$class">" . ucwords(strtolower($row["authorFirstName"] . " " . $row["authorLastName"])) . "</td><td class="$class">" . $row["description"] . "</td></tr>";
}
echo "</table>";
我是一名优秀的程序员,十分优秀!