gpt4 book ai didi

php - 在动态创建的表中 Bootstrap 交替行颜色

转载 作者:行者123 更新时间:2023-12-04 14:27:28 24 4
gpt4 key购买 nike

在 Stack Overflow 中我找到了 alternating row color .虽然这似乎适用于静态表,但我的结果是所有行都是粉红色的。我无法让它在带有 Bootstrap 的 PHP 动态创建的表上工作:

$mdbFile = "\myAccessDatabase.mdb";
$pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFile", "", "");
$query = $pdo->prepare("SELECT * FROM Table");
$query->execute();

echo "<table class='table table-striped table-bordered table-hover table-condensed table-responsive'>";
echo "<thead><tr>";
echo "<th>Last</th><th>First</th><th>XXX</th><th>YYY</th><th>ZZZ</th><th>WWW</th>";
echo "</tr></thead>";

for($i=0; $row = $query->fetch(); $i++){
echo '<tbody><tr>';
//echo "<th scope='row'>1</th>";
echo "<td>".$row['LAST']."</td>";
echo "<td>".$row['FIRST']."</td>";
echo "<td>".$row['XXX']."</td>";
echo "<td>".$row['YYY']."</td>";
echo "<td>".$row['ZZZ']."</td>";
echo "<td>".$row['WWW']."</td>";
}
echo "</tr></tbody></table>";

unset($pdo);
unset($query);

CSS:

.table-striped>tbody>tr:nth-child(odd)>td,
.table-striped>tbody>tr:nth-child(odd)>th {
background-color: pink;
}

最佳答案

问题可能是您的开始 tbody 标记在循环内。所以每一行都被视为第一行。尝试把它带到外面:

 echo '<tbody>';
for($i=0; $row = $query->fetch(); $i++){
echo '<tr>';
//echo "<th scope='row'>1</th>";
echo "<td>".$row['LAST']."</td>";
echo "<td>".$row['FIRST']."</td>";
echo "<td>".$row['XXX']."</td>";
echo "<td>".$row['YYY']."</td>";
echo "<td>".$row['ZZZ']."</td>";
echo "<td>".$row['WWW']."</td>";
echo '</tr>';
}
echo "</tbody></table>";

关于php - 在动态创建的表中 Bootstrap 交替行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42419663/

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