Erco21 Posted June 15, 2010 Share Posted June 15, 2010 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: Any help would be appreciated Link to comment https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/ Share on other sites More sharing options...
vbnullchar Posted June 15, 2010 Share Posted June 15, 2010 <pre> <?php $raw = file_get_contents('data.txt'); $delim = explode("\n", $raw); foreach($delim as $k => $d){ if($k%2){ $ident[] = $d; }else{ $user[] = $d; } } print_r($ident); print_r($user); exit(); ?> Link to comment https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/#findComment-1072512 Share on other sites More sharing options...
kratsg Posted June 15, 2010 Share Posted June 15, 2010 <?php $file_data = file('data.txt'); echo '<table>'; for($i=0;$i<sizeof($file_data)/2;$i+=2){ echo '<tr><td>'.$file_data[$i].'</td><td>'.$file_data[$i+1].'</td></tr>'; } echo '</table>'; This is off the top of my head. It should work I believe. Link to comment https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/#findComment-1072522 Share on other sites More sharing options...
Erco21 Posted June 15, 2010 Author Share Posted June 15, 2010 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 Link to comment https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/#findComment-1072532 Share on other sites More sharing options...
Erco21 Posted June 16, 2010 Author Share Posted June 16, 2010 bump Link to comment https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/#findComment-1072815 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2010 Share Posted June 16, 2010 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 Link to comment https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/#findComment-1072818 Share on other sites More sharing options...
Erco21 Posted June 16, 2010 Author Share Posted June 16, 2010 Yes, that shows all entries from txt, tho it still isnt in actual table, but im pretty satisfied with this too Thanks for help! Link to comment https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/#findComment-1072854 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2010 Share Posted June 16, 2010 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 Link to comment https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/#findComment-1072907 Share on other sites More sharing options...
Erco21 Posted June 16, 2010 Author Share Posted June 16, 2010 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 Link to comment https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/#findComment-1073146 Share on other sites More sharing options...
kenrbnsn Posted June 17, 2010 Share Posted June 17, 2010 Since you added a third value, you need to increment the counter by 3 each time instead of 2: <?php for ($i=0;$i<count($data);$i += 3) { ?> Ken Link to comment https://forums.phpfreaks.com/topic/204869-populate-table-on-page-from-file/#findComment-1073203 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.