Jump to content

[SOLVED] Creating tables with seperated values


jayrich

Recommended Posts

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!

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!

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!

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.