[英]Alternating and colored lines in php
我正在尝试在PHP的以下代码中在替换行上应用颜色,但是我已经在论坛中以各种形状和示例进行了尝试,但并没有解决。 我只需要留一条白线,另一条灰线,根据mysql查询填写即可。 我想在不使用CSS的情况下列出此列表。 有人可以帮忙吗?
<style type="text/css">
.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }
</style>
<table class="table-bordered" style="text-align: center; margin-left: -50px; font-family: Verdana, Geneva, sans-serif; margin-top: 20px;" border="0" cellspacing="1" cellpadding="2" align="center">
<table class="table-bordered" style="text-align: center; margin-left: -50px; font-family: Verdana, Geneva, sans-serif; margin-top: 20px;" border="0" cellspacing="1" cellpadding="2" align="center">
<thead>
<tr>
<th style="font-size: 1em; padding: 5px;">Client</th>
<th style="font-size: 1em; padding: 5px;">Tech</th>
<th style="font-size: 1em; padding: 5px;">Status</th>
<th style="font-size: 1em; padding: 5px;">Data</th>
<th style="font-size: 1em; padding: 5px;">Start</th>
<th style="font-size: 1em; padding: 5px;">End</th>
<th style="font-size: 1em; padding: 5px;">Total Hours</th>
</tr>
</thead>
<tbody>
<?php
$x++;
$class = ($x%2 == 0)? 'whiteBackground': 'graybackground';
echo "<tr class='$class'>";
foreach ($os as $c) {
$query = "SELECT nome from users where idUsers=$c->users_id";
$data= $this->db->query($query)->result();
echo '<tr >';
echo '<td >' . $c->nameclient . '</td>';
echo '<td >' . $data[0]->name . '</td>';
echo '<td >' . $c->status . '</td>';
echo '<td >' . date('d/m/Y', strtotime($c->startDate)) . '</td>';
echo '<td >' . $c->startTime . '</td>';
echo '<td >' . $c->endTime . '</td>';
$time_diff = strtotime($c->endTime) - strtotime($c->startTime);
$var= $time_diff/60;
echo '<td >'.date('H:i', mktime(0,$var)).'</td>';
echo '</tr>';
}
?>
我将展示使用%(或Mod)在简单数组上替换背景色的示例。
$arr = array("fred","tim","bob","jimmy");
echo "<table>";
$i=1;
foreach ($arr as $row) {
$class = ($i % 2) ? " odd": " even"; //even or odd?
echo "<tr class=" . $class . ">";
echo "<td>" . $row . "</td>";
echo "</tr>";
$i++;
}
echo "</table>"
?>
<style type="text/css">
.even, .odd {
color:white;
}
.even {
background-color: blue;
}
.odd {
background-color: red;
}
</style>
而且,如何在纯CSS中做到这一点。 (即IE 9或更高版本: https : //www.w3schools.com/cssref/sel_nth-child.asp )
$arr = array("fred","tim","bob","jimmy");
echo "<table>";
$i=1;
foreach ($arr as $row) {
echo "<tr>";
echo "<td>" . $row . "</td>";
echo "</tr>";
$i++;
}
echo "</table>"
?>
<style type="text/css">
.even, .odd {
color:white;
}
table tr:nth-child(even) {
background-color: blue;
}
table tr:nth-child(odd) {
background-color: red;
}
</style>
我认为许多人都不再使用HTML属性bgcolor了,但是您可以在TR标签bgcolor =“#cccccc”中使用它。 或者,您可以只使用内联样式的background-color:#cccccc;。
$highlight = false;
foreach ($os as $c)
{
$query = "SELECT nome from users where idUsers=$c->users_id";
$data= $this->db->query($query)->result();
$time_diff = strtotime($c->endTime) - strtotime($c->startTime);
$var= $time_diff/60;
$highlight = !$highlight;
echo '
<tr '.($highlight ? 'bgcolor="#cccccc"' : '').'>
<td >' . $c->nameclient . '</td>
<td >' . $data[0]->name . '</td>
<td >' . $c->status . '</td>
<td >' . date('d/m/Y', strtotime($c->startDate)) . '</td>
<td >' . $c->startTime . '</td>
<td >' . $c->endTime . '</td>
<td >'.date('H:i', mktime(0,$var)).'</td>
</tr>';
}
只需将您的CSS更改为此
<style type="text/css">
/* .whiteBackground { background-color: #fff; } */
/* .grayBackground { background-color: #ccc; } */
tbody tr:even{background-color:#fff;}
tbody tr:odd{background-color:#ccc;}
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.