In this tutorial I will show you how you can make a pretty cool html table generator, so that you can show off your excel files, since Excel outputs pretty messy HTML.
Here is the code and the explanation follows.
<?php
$file = "text.txt";
$delimiter = ",";
$column = array("Name", "Lastname", "Favorite Transport");
@$fp = fopen($file, "r") or die("Could not open file for reading");
while (!feof($fp))
{
$tmpstr = fgets($fp, 100);
$line[] = ereg_replace("\r\n", "", $tmpstr);
}
for ($i=0; $i < count($line); $i++)
{
$line[$i] = split($delimiter, $line[$i]);
}
?>
<html>
<head>
<title>Albert's Delimited Text to HTML Table Converter</title>
<style type="text/css">
table, body { border-collapse: collapse; font-family: verdana; font-size: 10px; }
</style>
</head>
<body>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<?php
for ($i=0; $i<count($column); $i++)
echo "<th>".$column[$i]."</th>";
?>
</tr>
<?php
for ($i=0; $i < count($line); $i++)
{
echo "<tr>";
for ($j=0; $j < count($line[$i]); $j++)
{
echo "<td>".$line[$i][$j]."</td>";
}
echo "</tr>";
}
fclose($fp);
?>
</table>
</body>
</html>
<?php $file = "text.txt";
Name of the text file containing the delimited text entries
$delimiter = ",";
The type of delimiter used, some files may use ; or other special characters that won't be used in normal text
$column = array("Name", "Lastname", "Favorite Transport");
Create an array of the column names.
@$fp = fopen($file, "r") or die("Could not open file for reading");
Added the @ to the start of the line so that it doesnt show the standard php error, but my custom error. Opens the file in read mode
while (!feof($fp))
{
While the file pointer is not at the end of the file, it gets one line from the file and moves the filepointer down one line
$tmpstr = fgets($fp, 100);
$line[] = ereg_replace("\r\n", "", $tmpstr);
}
Strip out all whitespace in the file, NOTE: this is dependant on the operating system you are using! EG: Unix based systems use \n only
for ($i=0; $i < count($line); $i++)
{
$line[$i] = split($delimiter, $line[$i]);
}
Splits each line into an array with all the text between the commas. Creates a 2-d array of the lines, so each line will now consist of all the columns, so cell A1 will be $line[0][0]
?>
<html>
<head>
<title>Albert's Delimited Text to HTML Table Converter</title>
<style type="text/css">
table, body { border-collapse: collapse; font-family: verdana; font-size: 10px; }
</style>
</head>
<body>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
This is setting up the HMTL file that we are going to create in the next step.
<?php
for ($i=0; $i<count($column); $i++)
echo "<th>".$column[$i]."</th>";
?>
</tr>
We use a 'for' loop which displays the column titles, using the th tag you get bold and centered cells without having to use extra tags
<?php
for ($i=0; $i < count($line); $i++)
{
echo "<tr>";
for ($j=0; $j < count($line[$i]); $j++)
{
echo "<td>".$line[$i][$j]."</td>";
}
echo "</tr>";
}
The above loops will display on a row, all the columns of data in that row, so it will display A1, B1, C1 etc. then start a new row and do the same until there are now lines left to be displayed
<?php fclose($fp); ?>
Always remember to close the file you worked on, its almost like a file on your desk, if you never close the file and put it away, after a while you end up with 100's of papers and files all mixed up :)
</table> </body> </html>
Finally we close the table and end the HMTL document.
Now you should understand how to make an html table generator with the power of PHP. A special thanks to Albert Gouws for writing and submitting this tutorial to scriptplayground.