Revision [5184]

This is an old revision of HandleCsvData made by NilsLindenberg on 2005-01-25 16:12:25.

 

Handle csv-data

This library is intended to hold functions which handle data from (and perhaps in some time) to *.csv files.

See the first action using this file:
- ShowCsv
 


The file needs to be placed under the name handlecsvdata.php in the /library folder (see WikkaCodeStructure).

To-do
- look for the difference of excel *.csv and normal csv
- possibility of sorting the entries?
- document!!
- formatting of the code

<?php
/*
* This library is intended to hold functions which deal with data from and to *.csv files.
*
* It includes the following functions:
*
* function GetCsvData($file, $separator=",") - Reads all data of a *.csv file.
* function StripHtml($data) - strips html from csv-data.
* function PrintCsvTable($data, $header="off", $tableclass="csvtable") - prints out a html table based on csv-data.
*
*/


/**
* Reads all data of a *.csv file.
*
* Based on a given filename every line of a file is read out and put into an array.
* Within this process, everything inside < and > is stripped off.
*
* Based on a given type of separator, every line is exploded and every entry of the line    
* put into the second dimension of the array, leading to the following structure:
* array[line][entry]
*
* @package  Library
* @subpackage   HandleCsvData
*
* @author       {@link http://it.php.net/manual/en/function.fgetcsv.php mjwilco at yahoo dot com} (basic code)
* @author       {@link http://wikka.jsnx.com/NilsLindenberg Nils Lindenberg} (error-handling and stripping html)
*
* @param        ? $file mandatory: name and path of the csv-file
* @param        char $separator optional: the separator used for dividing the entries
*               standard: ","
*
* @return       either a two-dimensional array containing the data of the file, or, in case of an error, FALSE
*
* @todo     - excel seem to make different *.csv files?
*       - error-handling for $rows = Striphtml
*       - detecting if $separator is only one char
*/

 
function GetCsvData($file, $separator=",")
{
    $endingtest = explode(".", $file);
    if ($endingtest[count($endingtest)-1] != "csv")  //checks if the ending of the file is *.csv
    {
        echo 'This file does not seem to be an csv-file. Please check the extension of it ('.$file.')'; # i18n
        return FALSE;
    }  
    if (file_exists($file))
    {
        $id = fopen($file, "r");
            if(!$id)
            {
                    echo 'The file you specified could not be opend. Please check the reading permission for the file ('.$file.')'; # i18n
                    return FALSE;
            }
            while ($data = fgetcsv($id, filesize($file), $separator)) //put each line into its own entry in the $rows array
            {        
                    $rows[] = StripHtml($data);
            }
            fclose($id);
            return($rows);
    }
    echo 'The file you specified was not found. Please check you input ('.$file.')'; # i18n
    return FALSE;
}

function StripHtml($data)
{
    if (is_array($data))
    {
        for ($i=0;$i<count($data);$i++)
        {
        while($safe[$i] != strip_tags($data[$i]))
            {$safe[$i] = strip_tags($data[$i]);}
        }  
        return($safe);
    }
    else echo 'The data transferred to StripHtmlinCsvData was no array and could therefore not be handeld! ('.$data.')'; # i18n
    return FALSE;      
}


function PrintCsvTable($data, $header="off", $tableclass="csvtable")
{
    if (is_array($data))
    {
        echo "<table class=\"".$tableclass."\">\n";

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

    for($i=1;$i<count($data);$i++)
    {
     echo '<tr>';
     for ($j=0; $j < count($data[$i]); $j++)
     {
      echo '<td>';
      echo $data[$i][$j];
      echo "</td>\n";
              }
             echo "</tr>\n";
        }
        echo "</table>\n";
    }
else echo 'The table could not be drawn because the data given to PrintCsvTable was no array.'; #i18n
}
?>



CategoryUserContributions

There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki