PHP: How to display in a table, information from a text file

root's picture

Php is able to read files and pass that information into a website. There are, of course, many ways to do this and below is just a tiny example.

Let's say you have in a text file, several lines in this format:

user@gzlinux $ cat files/servers-status.txt
server1#apache#10#up
server2#apache#20#down
server10#nfs#67#up
gzaix2#varnish#4#down
etc69#nginx#2#up

And you want to present this information in a php page with these tables: Hostname, Application, Number of Instances, Status.

Here is what your php page will need:

user@gzlinux $ cat envs.php
<html>
<head>
<title>GZ Media Environments</title>
</head>
<body>

[color=green]
<table class="table">
  <tr>
      <th>Hostname</th>
      <th>Application</th>
      <th>Number of instances</th>
      <th>Status</th>
  </tr>

 <?
$text = file_get_contents('files/servers-status.txt');
$lines = explode("\n",$text);

foreach ($lines as &$line) {
	list($hostname,$application,$no_instances,$status) = explode("#",$line);
	echo "<td class='small'>$hostname</td>";
	echo "<td class='small'>$application</td>";
	echo "<td class='small'>$no_instances</td>";
	echo "<td class='small'>$status</td>";
}
?>
</table>
[/color]

</body>
</html>

This solution is untested so do not use it in production until you are very satisfied with it.

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content

root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root