jayrich Posted May 24, 2008 Share Posted May 24, 2008 Hi everyone, I've been working on some code to display seperated values from an external file in a table. I've successfully finished the first stage by doing the following : <?php $filename = "/cgi-bin/values.db"; $id = fopen($filename, "r"); while ($data = fgetcsv($id, filesize($filename),"|")) $table[] = $data; fclose($id); echo "<table border=\"1\" class=\"sortable\" style=\"border-collapse: collapse\">\n<tr><td align=\"center\"><font face=\"Arial\" size=\"1\">Full Name</font></td><td align=\"center\"><font face=\"Arial\" size=\"1\">Email</font></td><td align=\"center\"><font face=\"Arial\" size=\"1 \">Username</font></td><td align=\"center\"><font face=\"Arial\" size=\"1\">ID</font></td><td align=\"center\"><font face=\"Arial\" size=\"1\">Address</font></td><td align=\"center\"><font face=\"Arial\" size=\"1\">Country</font></td><td align=\"center\"><font face=\"Arial\" size=\"1 \">Phone</font></td><td align=\"center\"><font face=\"Arial\" size=\"1\">Active</font></td><td align=\"center\"><font face=\"Arial\" size=\"1\">From</font></td><td align=\"center\"><font face=\"Arial\" size=\"1\">Until</font></td></tr>"; foreach($table as $row) { echo "<tr>"; foreach($row as $data) echo "<td><font face=\"Arial\" size=\"1\">$data</font></td>"; echo "</tr>\n"; } echo "</table>\n"; ?> For anyone who finds this code useful, the input file uses values seperated by pipes (|). You can replace this symbol with one of your own (common ones like commas also work well but I recommend pipes because it makes it easier to view seperated values in raw files. Just a tip!) You can see the seperation value at the end of the while statement (code line 4). The code works successfully and displays a lovely table with the columns : Full Name, Email, Username, ID, Address, Country, Phone, Active, From, Until showing all of the values from the input file. My current problem is that I only wish to display 2 of the fields in a different table. How do I go about making a table in php using only the 2 first fields (Full Name and Email)? If it can't be done then I guess that's the way it goes but I'd very much appreciate any help with this. I'm really struggling! Thank you! Link to comment https://forums.phpfreaks.com/topic/107074-solved-creating-tables-with-seperated-values/ Share on other sites More sharing options...
Barand Posted May 24, 2008 Share Posted May 24, 2008 while ($data = fgetcsv($id, filesize($filename),"|")) $table[] = array_slice($data,0,2); Link to comment https://forums.phpfreaks.com/topic/107074-solved-creating-tables-with-seperated-values/#findComment-548902 Share on other sites More sharing options...
jayrich Posted May 24, 2008 Author Share Posted May 24, 2008 Excellent! Thank you - that's exactly what I was looking for. Only one small problem though. Why am I getting an invalid argument in the foreach() statement on line 12 ($row as $data). Here's the code : <?php $filename = "/cgi-bin/values.db"; $id = fopen($filename, "r"); while ($data = fgetcsv($id, filesize($filename),"|")) $table[] = array_slice($data,0,2); $table[] = $data; fclose($id); echo "<table border=\"1\" class=\"sortable\" style=\"border-collapse: collapse\">\n<tr><td align=\"center\"><font face=\"Arial\" size=\"1\">Full Name</font></td><td align=\"center\"><font face=\"Arial\" size=\"1\">Email</font></td></tr>"; foreach($table as $row) { echo "<tr>"; foreach($row as $data) echo "<td><font face=\"Arial\" size=\"1\">$data</font></td>"; echo "</tr>\n"; } echo "</table>\n"; ?> Again, thank you. That's a huge step for me! Link to comment https://forums.phpfreaks.com/topic/107074-solved-creating-tables-with-seperated-values/#findComment-548914 Share on other sites More sharing options...
BlueSkyIS Posted May 24, 2008 Share Posted May 24, 2008 this is probably loading non-arrays into $table and when foreach runs into it, it gives you that error: $table[] = array_slice($data,0,2); $table[] = $data; Link to comment https://forums.phpfreaks.com/topic/107074-solved-creating-tables-with-seperated-values/#findComment-548923 Share on other sites More sharing options...
jayrich Posted May 24, 2008 Author Share Posted May 24, 2008 A big thank you to Barand and BlueSkyIS for solving this one! That $table[] = $data; statement is no longer necessary. Here is the finished code so that everyone can benefit : <?php $filename = "/cgi-bin/values.db"; $id = fopen($filename, "r"); while ($data = fgetcsv($id, filesize($filename),"|")) $table[] = array_slice($data,0,2); fclose($id); echo "<table border=\"1\" class=\"sortable\" style=\"border-collapse: collapse\">\n<tr><td align=\"center\"><font face=\"Arial\" size=\"1\">Full Name</font></td><td align=\"center\"><font face=\"Arial\" size=\"1\">Email</font></td></tr>"; foreach($table as $row) { echo "<tr>"; foreach($row as $data) echo "<td><font face=\"Arial\" size=\"1\">$data</font></td>"; echo "</tr>\n"; } echo "</table>\n"; ?> Work's a treat! Thank you everyone - PROBLEM SOLVED! Link to comment https://forums.phpfreaks.com/topic/107074-solved-creating-tables-with-seperated-values/#findComment-548931 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.