The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.
The Flash and PHP Bible has a forum for quick support.
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.
Follow Scriptplayground on Twitter (@scriptplay)
|
dev Wed Jun 20, 2007 10:14 pm
$line[$i] = split($delimiter, $line[$i]); // doesn't work for my set up
$line[$i] = explode($delimiter, $line[$i]); //works! |
|
james Fri Aug 10, 2007 7:50 am
testing
|
|
Live TV Tue Aug 21, 2007 12:33 pm
Why not just output as an unordered list and then just style it with css.
|
|
Sam Hale Fri Jan 25, 2008 11:59 am
The data I am using includes a delimeter right at the start of each line, which I don't think this code does.
How would I modify the code so it automatically included the data at the start. |
|
mkeefe Fri Jan 25, 2008 1:51 pm
@Sam, Are you saying your text has a "," at the beginning of each line? If so, you could change the delimiter to any other character.
|
|
Sam Hale Sat Jan 26, 2008 3:00 pm
Hi there;
Thanks for the super fast reply. My current output is : http://sam06.tfcph.com/ebay.php I have changed the delimiter to |#!#|, and the admin of the site says the problem is that the code does not encorporate the at the start of the line. Thanks again, Sam |
|
Sam Hale Sat Jan 26, 2008 3:02 pm
Sorry - it didn't show it right
"does not encorporate the " the * is a space. |
|
Sam Hale Sat Jan 26, 2008 3:03 pm
Didn't show it again!
"Does not encorporate the *<*b*r* /*>*" |
|
mkeefe Sat Jan 26, 2008 5:03 pm
No problem, btw, you can avoid code being run by using the HTML character codes (for now). In the new version which I am working on you will be able to properly format and display code without it being run.
|
|
Sam Hale Sat Jan 26, 2008 5:48 pm
Hi, thanks.
Unfortunetly I'm having trouble adding the *<*b*r*/*>* to appear at the start of each line- do ou know how? Kind Regards, Sam |
|
mkeefe Sat Jan 26, 2008 6:04 pm
I am not 100% sure where you want to add the break in the code, but if it is on the line, this looks like the correct place.
for ($j=0; $j < count($line[$i]); $j++) { echo "<td><br />".$line[$i][$j]."</td>"; } |
|
Sam Hale Sat Jan 26, 2008 7:05 pm
Hi there,
Unfortunetly I cannot get it to work, but I really appreciate your help. The source of data is like this: |#!#|220194007831|#!#|1201445897|#!#|1.83|#!#|N|#!#||#!#|EYE TOY - SEGA SUPERSTARS - PS2 (GAME ONLY) NO RESERVE!|#!#|1|#!#|1|#!#|8|#!#| |#!#|200193755444|#!#|1201449663|#!#|2.34|#!#|N|#!#||#!#|PS2 - Sega Superstars game|#!#|1|#!#|1|#!#|6|#!#| |#!#|160200810105|#!#|1201450855|#!#|2.34|#!#|N|#!#||#!#|sega superstars ps2|#!#|1|#!#|1|#!#|6|#!#| |#!#|120214964407|#!#|1201466327|#!#|3.34|#!#|N|#!#||#!#|PS2 GAME EYETOY KINETIC|#!#|2|#!#|1|#!#|6|#!#| |#!#|120214965407|#!#|1201466435|#!#|3.34|#!#|N|#!#||#!#|PS2 GAME EYETOY GROOVE|#!#|2|#!#|1|#!#|6|#!#| |#!#|120214966376|#!#|1201466532|#!#|3.34|#!#|N|#!#||#!#|PS2 GAME EYETOY PLAY 3|#!#|2|#!#|1|#!#|6|#!#| |#!#|270205879033|#!#|1201616487|#!#|2.83|#!#|N|#!#||#!#|Logitech Webcam with CD|#!#|2|#!#|1|#!#|6|#!#| |#!#|140201627180|#!#|1202035822|#!#|2.53|#!#|N|#!#||#!#|EyeToy Monkey Mania PlayStation 2|#!#|2|#!#|1|#!#|6|#!#| Do you know why it's not working? Kind Regards, Sam |
|
J.Mihai Mon Mar 17, 2008 9:10 am
Really useful, thanks
|
|
11 Mon Jan 19, 2009 12:25 pm
stinks
|
|
mkeefe Mon Jan 19, 2009 12:30 pm
@11 - Everyone is entitled to their opinion, but mentioning why you didn't like this tutorial would help the process moving forward.
Thanks, Matt |
|
forsooth Fri Nov 13, 2009 2:35 pm
Thank you -- very useful!
|
|
Ned Sat Aug 6, 2011 12:15 pm
Thank You, Worked Perfect! Exactly what I needed!!
|
©2004 - 2012 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN