Jump to content

Populate table on page from file


Erco21

Recommended Posts

Hi!

 

I need some help with populating a table (not database, just simple table) from a txt file

for instance:

i have txt file that contains:

User1
12345
User2
23342
User3
24324
User4
86786
User5
34534
User6
34534
User7
90778
User8
12327

 

and i want to populate a table on php page to look something like this:

ident.png

 

Any help would be appreciated

Link to comment
https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/
Share on other sites

Hmm, when i try to use first code, it doesnt show any tables, nor data, except something like this:

Array (

[0] => User1

)

Array (

[0] => 12345

)

 

or somethign similar to that, i forgot...

 

When using second code i get no tables, just first 2 users and their identifications, tho it looks like it is in table, it actually isnt, and main problem is that only first 2 get printed, and all others are ignored

This should work: (assumes the text file is called test.txt)

<?php
$data = array_map('trim',file('test.txt'));
echo "<table>\n";
for ($i=0;$i<count($data);$i += 2) {
     echo '<tr><td>' . $data[$i] . '</td><td>' . $data[$i+1] . "</td></tr>\n";
}
echo "</table>\n";
?>

 

Ken

My code generated a table, I just didn't format it. You would have seen this if you had looked at the generated source.

 

This code shows a formatted table:

<?php
$data = array_map('trim',file('testing.txt'));
echo "<table border='1'>\n";
echo "<tr><th>Heading1</th><th>Heading2</th></tr>\n";
for ($i=0;$i<count($data);$i += 2) {
     echo '<tr><td>' . $data[$i] . '</td><td>' . $data[$i+1] . "</td></tr>\n";
}
echo "</table>\n";
?>

 

Ken

wow, that works great!

So, i tried to add another collumn to table:

<?php
$data = array_map('trim',file('testing.txt'));
echo "<table border='1'>\n";
echo "<tr><th>User</th><th>HWID</th><th>Date</th></tr>\n";
for ($i=0;$i<count($data);$i += 2) {
     echo '<tr><td>' . $data[$i] . '</td><td>' . $data[$i+1] . '</td><td>' . $data[$i+2] . "</td></tr>\n";
}
echo "</table>\n";
?>

 

the collums get printed in order, but as txt file contains date in 3rd line, all data gets mixed up

txt file:

User1

12345

12.1.2010.

User2

89544

1.6.2000.

 

I sense that it's simple to fix that, but im just not that much familirat with php, so i need some assistance

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.