Author Topic: Is this good way to do it  (Read 598 times)

0 Members and 1 Guest are viewing this topic.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Is this good way to do it
« on: 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:

Code: [Select]
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:

Code: (php) [Select]
<!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.
« Last Edit: December 13, 2014, 11:27:05 pm by Code.Illusionist »
Vae Victis - suffering to the conquered

Offline Schalla

  • VIP
  • Peasant
  • *
  • Posts: 81
  • Cookies: 29
    • View Profile
Re: Is this good way to do it
« Reply #1 on: December 14, 2014, 12:55:15 am »
Code: (php) [Select]
<?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.
« Last Edit: December 14, 2014, 09:27:44 am by Kulverstukas »

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Is this good way to do it
« Reply #2 on: December 14, 2014, 02:33:50 pm »
Code: (php) [Select]
<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>
Easter egg in all *nix systems: E(){ E|E& };E

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Is this good way to do it
« Reply #3 on: December 14, 2014, 11:30:10 pm »
Thank you both for help.
Vae Victis - suffering to the conquered