php - Date range selection from form then export CSV from MySQL data with selected range -
i have database csv export working fine, want user able select date range export process.
here html part date select (format of date mm/dd/yyyy
):
<form action="csv_export_arp.php"> <p>select date range </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 works apart date range=
$sql_query = "select ticket_number 'ticket number', first_name 'first name', surname 'last name', email 'email address', product 'product', retailer 'retailer', dop 'date of purchase', message 'message', address 'postal address', date_format(created, '%m/%d/%y') 'date created', status 'status', action 'action', comment 'comment', resolution 'resolution' $table status='pending arp' , created >= date_format('from') , created < date_format('to')" ;
i having trouble linking values form (to , from) where
query date (status works fine), how can done? appreciated
, created >= date_format('from') , created < date_format('to')" ;
i feel important line, how can link query in date range selections on form?
edit:
this first part of script:
<?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 sanitized before use in query, or use pdo. $to_data = $_post['to']; //should sanitized before use in query, or use pdo. // connect database $link = mysql_connect($host, $user, $pass); mysql_select_db($db); require 'scripts/exportcsv_arp.inc.php'; $table="support_users"; // tablename want export csv mysql. exportmysqltocsv($table); ?>
this second part of script (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 'ticket number', first_name 'first name', surname 'last name', email 'email address', product 'product', retailer 'retailer', dop 'date of purchase', message 'message', address 'postal address', date_format(created, '%m/%d/%y') 'date created', status 'status', action 'action', comment 'comment', resolution 'resolution' $table status='pending arp' created >= date_format('" . $from_data . "', '%d/%m/%y') , created < date_format('" . $to_data . "', '%d/%m/%y')" ; // gets data database $result = mysql_query($sql_query); $fields_cnt = mysql_num_fields($result); $schema_insert = ''; ($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 $out = trim(substr($schema_insert, 0, -1)); $out .= $csv_terminated; // format data while ($row = mysql_fetch_array($result)) { $schema_insert = ''; ($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 $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 browser appropriate mime type, 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; } ?>
$from_data = $_post['from']; //should sanitized before use in query, or use pdo. $to_data = $_post['to']; //should sanitized before use in query, or use pdo.
and part of query pulled out should be
and created >= date_format('" . $from_data . "', '%d/%m/%y') , created < date_format('" . $to_data . "', '%d/%m/%y')
Comments
Post a Comment