Sure I already did, but again I need to learn what it does and they hardly (if at all) comment what their code is doing. Perhaps you could shed some light on what the lines of code in this example are doing:
<?php
if (!function_exists('fputcsv'))
{
function fputcsv(&$handle, $fields = array(), $delimiter = ';', $enclosure = '"')
{
$str = '';
$escape_char = '\\';
foreach ($fields as $value)
{
if (strpos($value, $delimiter) !== false ||
strpos($value, $enclosure) !== false ||
strpos($value, "\n") !== false ||
strpos($value, "\r") !== false ||
strpos($value, "\t") !== false ||
strpos($value, ' ') !== false)
{
$str2 = $enclosure;
$escaped = 0;
$len = strlen($value);
for ($i=0;$i<$len;$i++)
{
if ($value[$i] == $escape_char)
$escaped = 1;
else if (!$escaped && $value[$i] == $enclosure)
$str2 .= $enclosure;
else
$escaped = 0;
$str2 .= $value[$i];
}
$str2 .= $enclosure;
$str .= $str2.$delimiter;
}
else
$str .= $value.$delimiter;
}
$str = substr($str,0,-1);
$str .= "\n";
return fwrite($handle, $str);
}
}
function WriteCsv($fileName, $delimiter = ';', $records)
{
$result = array();
foreach($records as $key => $value)
$results[] = implode($delimiter, $value);
$fp = fopen($fileName, 'w');
foreach ($results as $result)
fputcsv($fp, split($delimiter, $result));
fclose($fp);
}
# =================== test ====================
define('CSV_SEPERATOR',';');
define('CSV_PATH','\\');
define('CSV_FILENAME','results.csv');
$records = array (array('aaa','bbb','ccc','dddd'),
array('123','456','789'),
array('"test1"', '"test2"', '"test3"')
);
$fileName = $_SERVER['DOCUMENT_ROOT'] . CSV_PATH . CSV_FILENAME;
WriteCsv ($fileName,';',$records);
echo '<a href="' . CSV_PATH . CSV_FILENAME . '" target="_blanc">CSV File</a>';
?>
Maybe there is a book out there I can read (excel reports with php & mysql for dummies)? Any hints on how you learned how to generate excel reports with php?