Assuming you're using PHP >= 5.1, have a look at fputcsv()
(http://php.net/fputcsv), and use the resource handler returned by
fopen('php://output', 'w')
On 23/09/2010, at 10:35 AM, Brendan Brink wrote:
> Hi there all,
>
> Using PHP to export to CSV files on the fly - and wondered what the
> best practice was to do in this regard...
>
> currently, im creating a variable to store the data to be exported as such
>
> $csv_output .= $order['product_code']."
> ".$order['product_description'].",".$store['supplier_name'].",".$order['product_quantity'].",".$order['import_format_time'].",$".$order['unit_cost'].","."$".round($order['unit_cost']*$order['product_quantity'],2)."\n";
>
> seperated by commas, terminated with new line
>
> and then exporting as such:
>
> $out = '';
>
> if (isset($_POST['csv_hdr'])) {
> $out .= $_POST['csv_hdr'];
> $out .= "\n";
> }
>
> if (isset($_POST['csv_output'])) {
> $out .= $_POST['csv_output'];
> }
>
>
> $filename = str_replace(" ","_",$_POST['csv_name']);
>
>
> header("Content-type: application/vnd.ms-excel");
> header("Content-disposition: csv" . date("Y-m-d") . ".csv");
> header("Content-disposition: filename=".$filename.".csv");
>
>
> print $out;
>
>
> ///
>
> issue i am facing is that the data that is being exported to the CSV
> file is entered by other users and may include commas in the text, or
> in the total currency amounts, eg. $42,000
>
> what is the best way to prevent any and all errors from happening in
> this regard, I tried using the semicolon, but this didnt seem to
> export well.
>
> any suggestions or assistance would be most appreciated...
>
> cheers
> brendan.
>
> --
> NZ PHP Users Group: http://groups.google.com/group/nzphpug
> To post, send email to [email protected]
> To unsubscribe, send email to
> [email protected]
---
Simon Welsh
Admin of http://simon.geek.nz/
Who said Microsoft never created a bug-free program? The blue screen never,
ever crashes!
http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e
--
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]