results in CSV
2 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: July 24, 2013 (RSS)
By Toledoh - July 23, 2013
Hi Guys,
I was looking at http://www.interactivetools.com/forum/forum-posts.php?postNum=2208599#post2208599 and wondering if it would be possible to set up a "report" which is basically a table - then send it as a csv attachment? Or a "click here to download as CSV"
$recordNum = mysql_insert_id();
// display thanks message and clear form
$alertsAndErrors = "Thanks, we've added that record!";
$csv = '';
$csv .= implode(', ', array_map('##list of field names##)) . "\n";
$csv .= implode(', ', array_map('_csvExport_escapeAsCSV', ##list of records##)) . "\n";
sendMessage(array(
'from' => "email@domain.id.au",
'to' => "email@domain.id.au",
'subject' => "Application Form",
'text' => "Text message content",
'attachments' => array(
'form.csv' => $csv
)
));
}
Tim (toledoh.com.au)
By gregThomas - July 24, 2013
Hi Tim,
If your generating your report using a getRecords function you could do something like this to create a CSV file of the data:
// load records from 'blog'
list($blogRecords, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'loadUploads' => false,
'allowSearch' => false,
'loadListDetails' => false,
'loadPseudoFields' => false
));
foreach($blogRecords as $key => $blog){
$csv = '';
if($key == '0'){
$csv .= "'".implode("', '", array_keys($blog)) . "'\r\n";
}
$csv .= "'".implode("', '", $blog) . "'\r\n";
}
sendMessage(array(
'from' => "test@example.com",
'to' => "test@example.com",
'subject' => "Application Form",
'text' => "Text message content",
'attachments' => array(
'form.csv' => $csv
)
));
This is just example code, so you will have to make a few changes to get it working how you want with your page.
So the foreach loop cycles through the blog records and generates a line for the CSV file. If it is the first line of the foreach loop then the array keys are used for the title fields of the CSV file, they are generated using the array_keys function which returns an array of an arrays keys.
You would also have to add a wrapper of code to detect when user has clicked a link to be e-mailed the CSV.
Let me know if you have any questions.
Thanks!
Greg
PHP Programmer - interactivetools.com