2

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);
?>
1
  • Could you perhaps also post the table that is being generated? Commented Jan 23, 2014 at 22:28

3 Answers 3

3

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>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude! Easier than expected :D
0

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>";
}

Comments

0

There are your problems:

  1. replace thead with table
  2. you should put tr and /tr tags inside of while loop
  3. add an elseif for when $row2['state'] is empty string. in this case output should be &nbsp; instead of empty string

1 Comment

Point 3 is very useful, thanks - something I'd not considered (at this point in time, my data is fully populated).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.