1 minute reading time (204 words)

CSV export using php

CSV export using php

A CSV referred to Comma separated values. This is widely supported format for transferring data between applications. Data can be exported with a .CSV file and it also can be imported by uploading .CSV file. However, in this blog we’ll see the implementation of CSV exports using php. Implementing a code for exporting data to CSV is useful in many applications. We have written this code in our product JGive to export donor data.

Note: This blog is purely targeted to developers!


Sample Image

Here goes the code:


$csvExport = null;

// Column name separated by using semicolon(;) as a delimiter 
$csvExport .= 'Id;Donor Name;Campaign Name;Donation amount';
$csvExport .= "\n";

// Code to write and download xsl file using php
header("Content-type: application/vnd.ms-excel");

// Set file name
$fileName = 'student-report'.date("Y-m-d");
header("Content-disposition: filename=" . $fileName . ".csv");

// Append 1st record, each column value separated by semicolon 
$csvExport .= '1;David;Go Green Save Earth;$100';

// Add new line after each record
$csvExport .= "\n";

// Append 2nd record
$csvExport .= '2;Josh;Health and Humanity;$50';

// Discards the contents of the output buffer.

// Echo the csv data
echo $csvExport."\n";



 I hope this will help you implement it in your products!



JTicketing 1.7 Alpha out now!
Subscribers, This one is for you!