$downloadID
and $dlDate
can be taken from an input dialog box. What is shown below is simply two input text boxes which are used to collect the desired parameters for the SQL query.
<form action="http://www.yourdomain.com/download-page/" method="post" name="FullDownload">
<label>Event ID Number:
<input type="text" name="downloadInfo" value=""></input></label>
<label>Event Date:
<input type="date" name="downloadDate" value=""></input></label>
<input type=submit Value="Full Download">
</form>
Then, in the ‘Download.php‘ file, I just need an SQL query to pull information from the Events Made Easy tables in the WordPress Database:
<?php
$downloadID = $_POST["downloadInfo"]; //frontend form Event ID Number
$dlDate = $_POST["downloadDate"]; //frontend form Event Date
$classDate = date("m-d-Y", strtotime($dlDate));
$fileName = $downloadID.'.csv'; //Names the *.csv file using the Event ID
$downloadSQL = "SELECT DISTINCT a.firstname AS 'First Name',a.lastname AS 'Last Name',a.MI,a.suffix AS Suffix,CONCAT_WS(' ', a.address1, a.address2) AS Address ,a.city AS City,a.state AS City,a.zip AS Zip,a.phone AS Phone,a.email AS EMAIL,b.event_id AS 'Class ID'
FROM wp_eme_bookings AS b
JOIN wp_eme_people AS a ON a.person_id = b.person_id
WHERE b.event_id = $downloadID ";
?>
First Name | Last Name | Address | City | State | Zip | Class ID |
a.firstname AS 'First Name',a.lastname AS 'Last Name'
to a.lastname AS 'Last Name',a.firstname AS 'First Name'
. Note that I use CONCAT_WS(' ', a.address1, a.address2)
to combine the two address fields into one field for the purpose of the download.<?php
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");
$fh = @fopen( 'php://output', 'w' );
global $wpdb; //call the WordPress Database
?>
<?php
$SQLresults= $wpdb->get_results($downloadSQL,ARRAY_A);//Run the SQL Query
$headerDisplayed = false;
foreach ( $SQLresults as $results )
{
$results = array('Date' => $classDate) + $results;
// Add a header row if it hasn't been added yet
if ( !$headerDisplayed ) {
// Use the keys from $results as the titles
fputcsv($fh, array_keys($results ));
$headerDisplayed = true;
}
// Put the data into the stream
fputcsv($fh, $results );
}
// Close the file
fclose($fh);
// Make sure nothing else is sent, our file is done
exit;
?>
The complete ‘Download.php‘ file can be placed in the WordPress current template directory. Then, a new page needs to be created (‘/download-page’ in the example above) specifying the ‘Download.php’ as the template.
Leave a Comment