带有动态内容的PHP表

时间:2014-01-23 22:23:05

标签: php mysql html-table

我正在写一小段PHP代码,它会显示一个表格; X轴将显示最近14天的窗口,Y轴将显示第一列中的计算机列表,每个日期单元格的内容将显示该日期的计算机状态(示例如下)。

|Hostname   |1   |2    |3  |
+-----------+----+---------+
|computer1  |OK  |DEAD |OK |
|computer2  |OK  |     |   |

我需要表格完全动态 - 所以如果今天添加新系统;它将显示并拥有今天的状态,但不是前一天。到目前为止一切都那么好 - 但是我的桌子没有正确关闭 - 所以计算机状态是垂直添加而不是水平添加,所以它看起来更像是这样:

|Hostname   |1   |2    |3  |
+-----------+----+---------+
|computer1  |OK  |     |   |
|computer2  |OK  |     |   |
|computer1  |DEAD|     |   |
|computer1  |OK  |     |   |

无法找到关闭桌子的好方法,同时保持准确,动态的内容。该表的代码包含在下面(这是缺少主机名列btw):

<?php
  $dates2 = mysql_query("SELECT day, month, year, hostname FROM log WHERE date >= DATE_ADD(CURDATE(), INTERVAL -14 DAY) ORDER BY date;");
?>

<?php
  echo "</thead>";
  echo "<tbody>";
  echo "<tr>";
  while ($row = mysql_fetch_array($dates2))
    {
      echo "<td><a href='/log/" . $row['hostname'] . ".log'>" . $row['hostname'] . "</a></td>";
      $state_date = mysql_query("SELECT state FROM log WHERE hostname = '" . $row['hostname'] . "' AND day = '" . $row['day'] . "' AND month = '" . $row['month'] . "' AND year = '" . $row['year'] . "' ORDER BY date;");
      while($row2 = mysql_fetch_array($state_date))
        {
          if(isset($row2['state']))
            {
              if ($row2['state'] == 'ok')
                 {
                   echo "<td bgcolor='#FF0000'>" . $row2['state'] . "</td>";
                 }
              elseif($row2['state'] == 'dead')
                 {
                   echo "<td bgcolor='#00FF00'>" . $row2['state'] . "</td>";
                 }
              else
                 {
                   echo "<td bgcolor='#FFD732'>" . $row2['state'] . "</td>";
                 }
            }
          else
            {
              echo "<td>Unknown</td>";
            }
         }
  echo "</tr>";
  }
  echo "</tbody></table>";
  mysql_close($con);
?>

3 个答案:

答案 0 :(得分:2)

你需要把&lt; TR&gt;部分在循环内。 TR是排。你把所有东西放在一排。

while (...)
{
   echo "<tr>";
   ...
   echo "</tr>";
}

答案 1 :(得分:0)

您需要遍历主机:

$hostnames = mysql_query("SELECT DISTINCT hostname FROM log WHERE date >= DATE_ADD(CURDATE(), INTERVAL -14 DAY) ORDER BY date;");
while($hostname_row = mysql_fetch_array($hostnames))
{
    $dates2 = mysql_query("SELECT day, month, year, hostname FROM log WHERE date >= DATE_ADD(CURDATE(), INTERVAL -14 DAY) AND hostname = '" . $hostname_row['hostname'] . "' ORDER BY date;");
    echo "<tr>";
    while ($row = mysql_fetch_array($dates2))
    {
      if(isset($row2['state']))
        {
          if ($row2['state'] == 'ok')
             {
               echo "<td bgcolor='#FF0000'>" . $row2['state'] . "</td>";
             }
          elseif($row2['state'] == 'dead')
             {
               echo "<td bgcolor='#00FF00'>" . $row2['state'] . "</td>";
             }
          else
             {
               echo "<td bgcolor='#FFD732'>" . $row2['state'] . "</td>";
             }
        }
      else
        {
          echo "<td>Unknown</td>";
        }
    }
    echo "</tr>";
}

答案 2 :(得分:0)

有你的问题:

  1. thead 替换为表格
  2. 你应该在while循环中加入 tr / tr 标签
  3. $ row2 ['state'] 为空字符串时添加 elseif 。在这种情况下,输出应该是&amp; nbsp; 而不是空字符串