Exporting to CSV with date range
Posted: Tue Mar 09, 2021 9:53 pm
I understand there is an Export to CSV function built-in to nuBuilder, but it doesn't fit my needs. I don't want my users that can eport to have access to "export" any other tables except for one. So I thought I 'd build a form that lets the user pick the start date and end date and then do the export. I'm trying to use this code:
//==============================
//Run the Export to a file here: I have already created code that I use for reporting that grabs the data from the existing TaxCustomers table, processes it and updates it in a temporary table. I know this process works, because I haven't changed it except for the initial SQL statement for what records it selects AND I see the temporary table get created in myPHPAdmin and it contains the updated data. (see Topic: https://forums.nubuilder.cloud/viewtopic.php?f=20&t=10866) for reference. So by the time the PHP script reaches this export section the records exist in a Temporary table and that table name is in the variable $tabid.
//==============================
What is happening is I get that popup window that is showing my output of my records then a bunch of other info from the database. SO I think the process is working, its just not generating the download dialog box like the built-in CSV Export function does. Any suggestions or help with this?
//==============================
//Run the Export to a file here: I have already created code that I use for reporting that grabs the data from the existing TaxCustomers table, processes it and updates it in a temporary table. I know this process works, because I haven't changed it except for the initial SQL statement for what records it selects AND I see the temporary table get created in myPHPAdmin and it contains the updated data. (see Topic: https://forums.nubuilder.cloud/viewtopic.php?f=20&t=10866) for reference. So by the time the PHP script reaches this export section the records exist in a Temporary table and that table name is in the variable $tabid.
//==============================
Code: Select all
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=ExpTaxCustomers.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings of my table
fputcsv($output, array('TaxCustomers_id', 'cust_prissn', 'cust_retdate', 'cust_lastname', 'cust_firstname', 'cust_midinit', 'cust_surname', 'cust_spouse', 'tax_office', 'tax_comments', 'count'));
// fetch the data from the temporary table $tabid - I know this table exists and has the correct data in it. I checked my nudebug to make sure its using the correct table name.
$expsql = "SELECT * From $tabid";
nuDebug($expsql);
$t = nuRunQuery($expsql);
// loop over the rows, outputting them this should output the rows to the output device.
while($r = db_fetch_array($t)){
fputcsv($output, $r);
} //end While Loop