Last updated August 31, 2012. Created by NancyDru on August 31, 2012.
Log in to edit this page.
I recently was tasked with creating a report of how many people signed up for the site by date. The query was simple, but creating the file was a bit of a challenge.
<?php
/**
* User sign up report, output as a CSV.
*/
function mymodule_signups_csv() {
// Prevent Devel from messing us up.
$GLOBALS['devel_shutdown'] = TRUE;
// Set the headers to indicate this is a CSV file.
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=user_signups.csv');
header('Pragma: no-cache');
header('Expires: 0');
// Create a file.
$output = fopen('php://output', 'w');
// Column names.
fputcsv ($output, array('Date', 'Count'));
$query = "SELECT DATE(FROM_UNIXTIME(created)) AS joined, COUNT(*) AS number FROM {users} "
. "WHERE status = 1 "
. "GROUP BY DATE(FROM_UNIXTIME(created)) "
. "ORDER BY created DESC ";
$result = db_query($query);
// Loop through the rows.
foreach ($result as $row) {
fputcsv($output, (array) $row);
}
fclose($output);
}
?>To get to this code was just a matter of creating a MENU_CALLBACK and adding a link to an existing page.
Note the 3rd line. Devel outputs its stuff after the </html> tag, so Excel saw that as data. That simple statement just turns Devel off for this single page load.