EvilZone
Programming and Scripting => Web Oriented Coding => : Code.Illusionist December 13, 2014, 11:26:53 PM
-
I know that topic title is kinda bad, but whateva. Anyway, I have one file named test.txt and Inside it I have content like this:
Evilzone , http://www.evilzone.org , 789456
Krstarica , http://www.krstarica.com , 789456
Facebook , http://www.facebook.com , 147852
Twitter , http://www.twitter.com , 258963
First one is forum Name, second one is URL and third one is password. Now, my question is, is it good to use this code to print table for HTML:
<!doctype html>
<html>
<head>
</head>
<body>
<?php
$file = "test.txt";
$fContent = file_get_contents($file);
$fContent = explode("\n",$fContent);
foreach($fContent as $key => $value) {
$tEverything[$key] = explode(",",$value);
}
echo "<table width=600 border=1>";
for($i = 0;$i < count($tEverything);$i++) {
echo "<tr>";
for($j = 0; $j < 3; $j++) {
echo "
<td>{$tEverything[$i][$j]}</td>";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
I am curious , is there shorter way to do this, but to print result as I wanted.
-
<?php
$file = "test.txt";
$fContent = file_get_contents($file);
$fContent = explode("\n",$fContent);
?>
<!doctype html>
<html>
<head>
<title>title 'n stuff</title>
</head>
<body>
<table width="600" border="1">
<?php foreach($fContent as $row):?>
<tr>
<?php foreach(explode(",",$value) as $value):?>
<td><?=$value?></td>
<?php endforeach;?>
</tr>
<?php endforeach;?>
</table>
</body></html>
Since you asked for a short version, I didnt even split up the explode.
However, never split up logical code (open, calculate, etc) and print. Always separate the sections, that way your
code is more clean.
This version is the shortest code I can imagine at least.
-
<table>
<?php
if (($fp = fopen('test.txt', 'r'))) {
while (($data = fgetcsv($fp))) {
echo '<tr>';
foreach ($data as $value)
echo "<td>$value</td>";
echo '</tr>';
}
fclose($fp);
}
?>
</table>
-
Thank you both for help.