Search the Community
Showing results for tags 'date range'.
-
I have a database CSV export working fine, but I want the user to be able to select a date range (date created) for the export process. Here is my HTML part for the date select (format of date on the form is mm/dd/yyyy): <form action="csv_export_arp.php"> <p>Select a date range from </p><label style="color:#FFF;" for="from">From</label> <input type="text" id="from" name="from" value="from" /> <label style="color:#FFF;" for="to" >to</label> <input type="text" id="to" name="to" value="to"/> <input name="export" type="submit" value="Export Pending ARP CSV" /> </form> My CSV export which works apart from the date range (two different files for script: Part 1 (connect ot db) <?php $host = 'xxxxx'; // MYSQL database host adress $db = 'wxxxxx'; // MYSQL database name $user = 'xxxxx'; // Mysql Datbase user $pass = 'xxxxxx'; // Mysql Datbase password $from_data = $_POST['from']; //Should be sanitized before use in the query, or use PDO. $to_data = $_POST['to']; //Should be sanitized before use in the query, or use PDO. // Connect to the database $link = mysql_connect($host, $user, $pass); mysql_select_db($db); require 'scripts/exportcsv_arp.inc.php'; $table="support_users"; // this is the tablename that you want to export to csv from mysql. exportMysqlToCsv($table); ?> Part two(exportcsv_arp.inc.php): <?php function exportMysqlToCsv($table,$filename = 'export_arp.csv') { $csv_terminated = "\n"; $csv_separator = ","; $csv_enclosed = '"'; $csv_escaped = "\\"; $sql_query = "SELECT ticket_number AS 'Ticket number', first_name AS 'First name', surname AS 'Last name', email AS 'Email address', product AS 'Product', retailer AS 'Retailer', dop AS 'Date of purchase', message AS 'Message', address AS 'Postal address', DATE_FORMAT(created, '%m/%d/%Y') AS 'Date created', status AS 'Status', action AS 'Action', comment AS 'Comment', resolution AS 'Resolution' FROM $table WHERE status='Pending ARP' created >= DATE_FORMAT('" . $from_data . "', '%d/%m/%Y') AND created < DATE_FORMAT('" . $to_data . "', '%d/%m/%Y')" ; // Gets the data from the database $result = mysql_query($sql_query); $fields_cnt = mysql_num_fields($result); $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i))) . $csv_enclosed; $schema_insert .= $l; $schema_insert .= $csv_separator; } // end for $out = trim(substr($schema_insert, 0, -1)); $out .= $csv_terminated; // Format the data while ($row = mysql_fetch_array($result)) { $schema_insert = ''; for ($j = 0; $j < $fields_cnt; $j++) { if ($row[$j] == '0' || $row[$j] != '') { if ($csv_enclosed == '') { $schema_insert .= $row[$j]; } else { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; } } else { $schema_insert .= ''; } if ($j < $fields_cnt - 1) { $schema_insert .= $csv_separator; } } // end for $out .= $schema_insert; $out .= $csv_terminated; } // end while header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Length: " . strlen($out)); // Output to browser with appropriate mime type, you choose //header("Content-type: text/x-csv"); //header("Content-type: text/csv"); header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=$filename"); echo $out; exit; } ?> I can't figure out how to get the query to link in with the selected date range from my form (at the top), I feel this is the important line of my csv export script and this is what I tried, probably quite a way off with it (if the date range query part is removed, the CSV export works fine with the right staus only (pending arp) exported): FROM $table WHERE status='Pending ARP' created >= DATE_FORMAT('" . $from_data . "', '%d/%m/%Y') AND created < DATE_FORMAT('" . $to_data . "', '%d/%m/%Y')" ; Any guidance on getting this working would be much appreciated.
- 26 replies