Revision [4919]
This is an old revision of HandleCsvData made by NilsLindenberg on 2005-01-21 17:04:42.
Handle csv-data
This library is intended to hold functions which handle data from (and perhaps in some time) to *.csv files.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!!
- error-handling for $rows = Striphtml
- formatting of the code
<?php
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.')';
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.')';
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.')';
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.')';
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.';
}
?>
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.')';
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.')';
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.')';
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.')';
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.';
}
?>