Revision [3880]

This is an old revision of ShowCsv made by NilsLindenberg on 2005-01-02 17:12:19.

 

Showing the content of a csv-file


Last edited by NilsLindenberg:
version 0.2 + bug-description
Sun, 02 Jan 2005 17:12 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

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)
*
* 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 type? optional if set to "on", the first entry will be shown strong. 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++)
   {
    echo "<td>";
    if ($header == 'on') echo "<strong>";
    echo $entry[$j];
    if ($header == 'on') echo "</strong>";
    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