Revision [3954]

This is an old revision of ShowCsv made by NilsLindenberg on 2005-01-04 02:04:41.

 

Showing the content of a csv-file


Last edited by NilsLindenberg:
code-change in the header-section
Tue, 04 Jan 2005 02:04 UTC [diff]


if you save this as actions/showcsv.php, you have the usage:

{{showcsv file="uploads/file.csv" seperator="," header="on"}}

- file is the name of the file (needs to be on the same server as the wikka?)
- seperator is the seperator for the entries
- if you set header="on", the first entry in the file will be printed strong

Bugs

At the moment, there is a bug if you use a , as seperator or in your file. the explode of the entries on a line won't work. I have no clue why. You can see it here.


<?php
/**
* Prints a table based on a csv-textfile.
*
* With this action a csv file, which has to be on the same server as the wikki, is read and its content presented in a table.
*
* @author mjwilco at yahoo dot com
* @author Nils Lindenberg (http://wikka.jsnx.com/NilsLindenberg)
* @author Stefan Lindenberg (http://wikka.jsnx.com/NilsLindenberg)
*
* file string mandatory  the name of the file which should be shown
*
* delimeter char optional  the delimeter of the entries in the csv-file. standard is ","
*
* header string optional  if set to "on", the first entry will be used as header. Standard is "off";
*/


//parameters
$filename = $vars['file'];
if ($vars['seperator']) $seperator = $vars['seperator'];
else $seperator = ",";
if ($vars['header']) $header = $vars['header'];
else $header = "off";

if (file_exists($filename))
{
   $id = fopen($filename, "r");
   while ($data = fgetcsv($id, filesize($filename)))
   {
    $table[] = $data; //put each line into its own entry in the $table array
   }
   fclose($id);

   //print the table
   echo "<table border=1>\n";

   //first entry handeld seperate, possible header
   $entry = explode($seperator, $table[0][0]);
   echo "<tr>\n";
   for ($j = 0; $j < count($entry); $j++)
   {
    if ($header == 'on') echo "<th>";
    else echo "<td>";
    echo $entry[$j];
    if ($header == 'on') echo "</th>\n";
    else echo "</td>\n";
   }
   echo "</tr>\n";

   for($i=1;$i<count($table);$i++)
   {
    $entry = explode($seperator, $table[$i][0]);
    for ($j=0; $j < count($entry); $j++)
    {
     echo "<td>";
     echo $entry[$j];
     echo "</td>\n";
    }
    echo "</tr>\n";
   }
   echo "</table>\n";
}
else print("File not found.");
?>
There are 9 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki