As I have already mentioned in a previous article here, lately PHP has attracted my attention. Thus, today I will show how to build a small HTML table from a txt file, using each line in the file as a different row. Something like this:
Let’s see what do we need? A file to read and free time to write 31 lines of code. Not much, eh?
Actually, we check whether the file we need actually exists, can we open it and if the answer is yes – we create a table with a row for each line of the file.
Beautiful in its own way. Here comes the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<html> <body> <? if(!file_exists("read_me.txt")) { echo "The file from above cannot be found!"; exit; } $fp = fopen("read_me.txt", "r"); if(!$fp) { echo "Somehow the file cannot be opened! :)"; exit; } echo "<table border = 4>"; $counter = 1; while(!feof($fp)) { $zeile = fgets($fp); echo "<tr><td>$counter</td>"; echo "<td>$zeile</td>"; $counter++; } echo "</table>"; fclose($fp) ?> </body> </html> |
Enjoy it!