我正在尝试根据每行的时间对表格进行条带化。例如:
Blah - time 1
Blah2 - time 1
Blah3 - time 2
Blah4 - time 3
Blah5 - time 4
我需要时间1为一种颜色(例如蓝色)时间2为黑色,时间为3以切换回蓝色。我试图根据时间的变化切换颜色。到目前为止我的PHP:
while($main_Row = $main_Results->fetch_assoc()){
if( $pickup_Time == $main_Row['pickup_Time'] ){
$stripe = "blue";
}
else{
$stripe = "black";
$pickup_Time = $main_Row['pickup_Time'];
}
echo '<tr class="'.$stripe.'">';
echo '<td>Blah'.$i.'</td>';
echo '<td>'.$main_Row['pickup_Time'].'</td>';
echo '</tr>';
}
我目前所拥有的并没有给我我想要的结果。我觉得我错过了什么,任何帮助都会非常感激。
答案 0 :(得分:2)
$pickup_Time = "";
$stripe = "";
while($main_Row = $main_Results->fetch_assoc()){
if( $pickup_Time != $main_Row['pickup_Time'] ){
$stripe = $stripe == "blue" ? "black" : "blue";
}
echo '<tr class="'.$stripe.'">';
echo '<td>Blah'.$i.'</td>';
echo '<td>'.$main_Row['pickup_Time'].'</td>';
echo '</tr>';
$pickup_Time = $main_Row['pickup_Time'];
}
答案 1 :(得分:0)
快速地将某些东西扔在一起,它有点冗长,但我想让一般意图和过程清晰。
这将按行进行条带化,每列条带化,您可以将类添加到td。如果要在再次循环之前更改条纹数量,请将模数更改为您想要的任何数字。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.red {
background-color: red;
}
.blue {
background-color: blue;
}
</style>
</head>
<body>
<?php
$mysqli = new mysqli('', '', '', '');
$qry = "SELECT * FROM data";
$result = $mysqli->query($qry);
$counter = 1;
echo "<table>";
while ( $row = $result->fetch_assoc() ) {
$class = "";
if( $counter % 3 == 1 ) {
$class = "blue";
} else if ( $counter % 3 === 2 ) {
$class = "red";
}
echo '<tr class="' . $class . '">';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['id'] . '</td>';
echo '</tr>';
$counter += 1;
}
echo "</table>";
$result->free();
$mysqli->close();
?>
</body>
</html>