Reputation: 863
I'm writing a small bit of PHP code which will display a table; the X axis will display a window of the last 14 days and the Y axis will display a list of computers in the first column, the content of each date cell will show the computers state on that date (example below).
|Hostname |1 |2 |3 |
+-----------+----+---------+
|computer1 |OK |DEAD |OK |
|computer2 |OK | | |
I need the table to be fully dynamic - so if a new system is added today; it will show and have a state for today, but not for any previous day. So far so good - however my table isn't getting closed properly - so computer states are getting added vertically, rather than horizontally, so it looks more like this:
|Hostname |1 |2 |3 |
+-----------+----+---------+
|computer1 |OK | | |
|computer2 |OK | | |
|computer1 |DEAD| | |
|computer1 |OK | | |
Can't figure out a good way of closing the table, whilst maintaining accurate, dynamic content. The code for the table is included below (this is missing the hostname column 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);
?>
Upvotes: 2
Views: 245
Reputation: 35
There are your problems:
Upvotes: 0
Reputation: 3352
You'll need to iterate through the hosts:
$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>";
}
Upvotes: 0
Reputation: 8659
You need to put the <TR> part inside the loop. TR is the row. You're putting everything on one row.
while (...)
{
echo "<tr>";
...
echo "</tr>";
}
Upvotes: 3