Revision [5317]

This is an old revision of HandleCsvData made by NilsLindenberg on 2005-01-28 11:45:38.

 

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!!

<?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;      
}

/**
* Prints a html-table based on the content of a 2-dimensional array.
*
* Based on a given 2-dimensional array, with the structure [line][line-entry] a html table is printed.
* When the parameter $header is set to on, the entries of the first line will be used as columm-headers.
* Standard of this feature is off.
*
* You can determine the look of the table via css. The parameter $tableclass awaits the name of your css entry.
* To determine the defaults, add a csvtable-class to your style-sheet, like the example below:
* .csvtable  { border =1;}
*
*
* @package  Library
* @subpackage   HandleCsvData
*
* @author       {@link http://wikka.jsnx.com/NilsLindenberg Nils Lindenberg}
*
* @param        array $data mandatory: a twodimensional array with the data for the table
* @param        string $header optional: columm-header "on" (anything else will be "off"). Standard is "off".
* @param        string $tableclass optional: css-class for the table. Standard is "csvtable".  
*              
*
* @return       either nothing, or, in case of an error, 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