【问题标题】:How can I add more than just one class to table rows in PHP? [closed]如何在 PHP 中向表行添加多个类? [关闭]
【发布时间】:2013-06-18 21:57:57
【问题描述】:

我正在尝试修复其他人的代码。现在我正在处理一个动态创建的表,并想为每个第二行添加一个不同的类。例如,第 3 行和第 5 行的背景应该是白色的,而第 2 4 和第 6 行的背景应该是灰色的。

谁能告诉我如何在代码中添加这样的第二类?

我不确定这个问题是否需要代码 - 这是带有表格的部分。如果这还不够,请让我知道,因为我的 php 技能不好,我什至不确定需要什么来理解这个问题。如前所述,这不是我的代码...

   $arr = array();
foreach ($them as $z => $e) {
  $_them[$e['id']] = $e;
  if (count($arr) < 50) {
    $res = mysql_query('SELECT COUNT(*) FROM `forum_comments` WHERE `id_them` = ' . $e['id'] . '');
    $row = mysql_fetch_row($res);
    $total = $row[0];

    $arr[$e['id']] = $total;
  } else {
    $res = mysql_query('SELECT COUNT(*) FROM `forum_comments` WHERE `id_them` = ' . $e['id'] . '');
    $row = mysql_fetch_row($res);
    $total = $row[0];

    if ($total > $this->tools->array_val_min($arr)) {
      $arr[$this->tools->array_key_min($arr)] = $total;
    }
  }
}

arsort($arr);
$arr = array_chunk($arr, 25, true);
if (!empty($_GET['p'])) {
  $num = (int) $_GET['p'];
  if ($num == 1) {
    $arr = $arr[0];
  }
  if ($num > 1) {
    $arr = $arr[1];
  }
}

$ret = '
        <table class="forum_table">
            <tr class="forum_table_title">
                <th id="them">' . l::themes() . '</th>
                <th id="date">' . l::added() . '</th>
                <th id="users">' . l::guests() . '</th>
                <th id="commets">' . l::answers() . '</th>
                <th id="last">' . l::last_comments() . '</th>
            </tr>
            <tr class="empty_td">
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
    ';

【问题讨论】:

  • 除非你显示代码...
  • 一些代码会很有用...
  • 其他类可以用空格隔开:class="class1 class2"
  • 没有代码很难知道,但你也可以看看 CSS3 中的伪选择器,尤其是 nth-child
  • 他已经发布了代码。我认为反对票应该删除:)

标签: php css


【解决方案1】:

只需使用一些 css,它会自动应用它:

.your_table tr:nth-child(odd) { background-color:#00000; }
.your_table tr:nth-child(even) { background-color:#ffffff; }

【讨论】:

  • & @GCyrillus & swapnesh 感谢您的回复,我不知道CSS有这个功能,发现不仅有(偶数)而且还有(奇数)作为一个选项非常有帮助.
【解决方案2】:

逻辑方式 -

这样的事情甚至 - $i%2 == 0

CSS 方式 -

td {
  background: white;
}
td:nth-child(even) {
  background: grey;
}

【讨论】:

【解决方案3】:

Css 可以为你做到:

tr {
       background:white;
    }
tr:nth-child(even) {
       background:grey;
    }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 2021-04-23
  • 1970-01-01
相关资源
最近更新 更多