Tuesday, December 15, 2009
php generate csv on the fly
Useful, quick and easy method to generate CSV file on the fly with PHP
credit to : http://www.zedwood.com/article/130/generate-csv-spreadsheet-with-php
$filename = date("Ymd")."_your_file_name.csv";
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
$field_arr = array('productid','userid','quantity','cost','description');
foreach($field_arr as $val)
rtrim_csv_out($val);
echo "\n";
for($i=0; $i<10; $i++)
{
rtrim_csv_out( rand( 0,1000) );
rtrim_csv_out( rand( 0, 100) );
rtrim_csv_out( rand(50, 150) );
rtrim_csv_out( sprintf("%1.2f", rand(0,23)/100 ) );
rtrim_csv_out( "description test $i" );
echo "\n";
}
function rtrim_csv_out($str)
{
echo '"'.rtrim(str_replace('"','""',$str)).'",';
}
best,
PS. ဘာမွ မဟုတ္ဖူး။ ေမ့သြားမွာ စိုးလို႔။
No comments:
Post a Comment