The Techjoomla Blog

Stay updated with all the latest happenings at Techjoomla. From news about the developments in your favourite extensions to Tips & Tricks about the Joomla CMS, Framework & Development.
Font size: +
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:

<?php

$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.
ob_clean();

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

?>

 

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

 

 

8
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

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