简体   繁体   English

PHP格式表颜色?

[英]PHP Format colour of table?

I have a table coded a little like below: 我有一个编码如下的表格:

echo "<table class='lt show'>
      <tr class='h'>
      <td class='ltid' title='Position'>#</td>
      <td class='ltn' title='Name'>Name</td>";

$i = 0;

while ($i < $num){
    $pos = $i + 1;

    $name = mysql_result($result, $i, "name");

    $bgcolor = 'bgcolor="'.$color.'"';

    if ($pos <=  19) $color = "#FFF";

    echo "<tr $bgcolor class='even'><td class='ltid'>$pos</td>
          <td class='ltn'><a href='teams.php?id=$id'>$name</td>";

This returns a table of 20 rows each containing different positions and names. 这将返回一个包含20行的表,每个表包含不同的位置和名称。 How can I make the bottom two rows / the 20 be red to indicate they're at the bottom of the table? 如何使底部的两行/ 20变为红色以指示它们位于表格的底部?

Note MySQL functions have been depreceated in favour of PDO. 注意MySQL函数已不赞成使用PDO。

Saying that, this is how you do it 这样说,这就是你的做法

$num= mysql_num_rows ($result);
while ($i < $num){
$pos = $i + 1;

$name = mysql_result($result, $i, "name");

$bgcolor = 'bgcolor="'.$color.'"';

if ($pos <  $result-2) $color = "#FFF";

echo "<tr $bgcolor class='even'><td class='ltid'>$pos</td>
      <td class='ltn'><a href='teams.php?id=$id'>$name</td>";
}

You could use the mysql_num_rows() function to compute the number of rows in your result set, then minus two from the total to determine the loop index to set the colour to red: 您可以使用mysql_num_rows()函数计算结果集中的行数,然后从总数中减去2确定循环索引以将颜色设置为红色:

http://php.net/manual/en/function.mysql-num-rows.php

Note these functions are deprecated though in favour of mysqli and PDO. 请注意,尽管赞成使用mysqli和PDO,但不赞成使用这些功能。 Not so sure if that concerns you or not. 不确定是否与您有关。

Or you could do an SQL count directly: 或者您可以直接进行SQL计数:

SELECT COUNT(1) FROM Table;

Which would return you a set count, the choice is yours - without an awareness of the total numer of rows being iterated, there is no way of determining which point to set the row colour. 这将返回给您一个固定的计数,选择​​的是您自己的-在不知道要迭代的行总数的情况下,无法确定设置行颜色的点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM