php - php中的交替和彩色线条

标签 php html css

我试图在以下 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>

关于php - php中的交替和彩色线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43923824/

相关文章:

php - 将 mysql 从 5.1 更新到 5.6,但 php 信息仍然显示 5.1 客户端版本

Php:如何计算 zip 的 Adler32 校验和?

c# - WP8 : Setting Image width to device width in Webbrowser Control

jquery - 如何使输入下的所有内容都具有静态位置?

javascript - 在 Ajax 启停事件时禁用和启用提交的响应方法

CSS slider 表现异常

PHP、PDO、MySQL,通知 : Trying to get property of non-object

php - 无法覆盖 s3 中的内容配置 header

javascript - 粘性页脚与图像 slider 不合适

javascript - 添加到 "this"对象变量时 jQuery addClass 不起作用