note 36212 modified in function.fgetcsv by aidan
| From: | aidan@php.net | Date: | Thu, 01 Jul 2004 01:01:32 +0000 |
| Subject: | note 36212 modified in function.fgetcsv by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72208@lists.php.net to get a copy of this message | ||
I wrote this little thingy to allow the customer to upload csv-files to his page which will then
automatically be converted to a php-include-file with a html-table for inclusion in his homepage.
Its a little buggy since it can't handle rowspan and such, but otherwise it works well.
<?php
function csv2php($filename, $delim=';',$path)
{
$row = 0;
$dump = array();
$once = 0;
$max_cols = 0;
// open file - get number of columns - make data Array
$f = fopen ($filename,"r");
$size = filesize($filename)+1;
while ($data = fgetcsv($f,$size,$delim,''))
{
for($o=0;$o<sizeof($data);$o++)
{
if($once==0)
$max_cols++;
}
$dump[$row] = $data;
if($once==0)
$once=1;
$row++;
}
fclose ($f);
/////// make text for php-include file
$printtext="<table border='0' cellspacing='0'
cellpadding='0'>\n";
/////// walk the rows
for($x=0;$x<sizeof($dump);$x++)
{
$printtext.="<tr>\n";
/////// !!!! Table is built with as many columns as the first row has !!!!!!
for($y=0,$limiter=0;(($y<sizeof($dump[$x])) || ($limiter<$max_cols));$y++,$limiter++)
{
$printtext.="\t<td>";
if($dump[$x][$y]!='')
$printtext.=$dump[$x][$y];
else
$printtext.=" ";
$printtext.="</td>\n";
}
$printtext.="</tr>\n";
}
$printtext.="</table>\n";
$ident = write_dammit($printtext,$path);
return $ident;
}
function write_dammit($printtext,$path)
{
$ident=time();
$filename=$ident;
$filename .= ".inc.php";
$filecontents = $printtext;
$fp = fopen("$path/$filename","w"); // if in windows you have to chace
the / to \\
fwrite($fp,$filecontents);
fclose($fp);
return $ident;
}
$ident = csv2php("table.csv",";","C:/your_include_folder/");
require($ident.".inc.php");
?>
--was--
I wrote this little thingy to allow the customer to upload csv-files to his page which will then
automatically be converted to a php-include-file with a html-table for inclusion in his homepage.
Its a little buggy since it can't handle rowspan and such, but otherwise it works well.
function csv2php($filename, $delim=';',$path)
{
$row = 0;
$dump = array();
$once = 0;
$max_cols = 0;
// open file - get number of columns - make data Array
$f = fopen ($filename,"r");
$size = filesize($filename)+1;
while ($data = fgetcsv($f,$size,$delim,''))
{
for($o=0;$o<sizeof($data);$o++)
{
if($once==0)
$max_cols++;
}
$dump[$row] = $data;
if($once==0)
$once=1;
$row++;
}
fclose ($f);
/////// make text for php-include file
$printtext="<table border='0' cellspacing='0'
cellpadding='0'>\n";
/////// walk the rows
for($x=0;$x<sizeof($dump);$x++)
{
$printtext.="<tr>\n";
/////// !!!! Table is built with as many columns as the first row has !!!!!!
for($y=0,$limiter=0;(($y<sizeof($dump[$x])) || ($limiter<$max_cols));$y++,$limiter++)
{
$printtext.="\t<td>";
if($dump[$x][$y]!='')
$printtext.=$dump[$x][$y];
else
$printtext.=" ";
$printtext.="</td>\n";
}
$printtext.="</tr>\n";
}
$printtext.="</table>\n";
$ident = write_dammit($printtext,$path);
return $ident;
}
function write_dammit($printtext,$path)
{
$ident=time();
$filename=$ident;
$filename .= ".inc.php";
$filecontents = $printtext;
$fp = fopen("$path/$filename","w"); // if in windows you have to chace
the / to \\
fwrite($fp,$filecontents);
fclose($fp);
return $ident;
}
$ident = csv2php("table.csv",";","C:/your_include_folder/");
require($ident.".inc.php");
http://php.net/manual/en/function.fgetcsv.php