我有一个php文件,它连接到下面的数据库,它工作得很好,并显示了一个结果表,用户指定一个带有下拉菜单的字段,并在其中有一个Jquery datepicker,用于选择查询中的日期范围。乱七八糟(很抱歉!)但功能强大。问题是,我想添加一个下载按钮,这样最终用户就可以将屏幕上的内容输出到包含标题的.csv。我在这方面的尝试揭示了我的业余编程方法(即我做不到!)我在摆弄table2excel插件,我确信更简单的是,我不确定如何在保留现有代码的同时编写.csv函数,或者是否可以绕过执行代码来完成它。我在.csv导出的大多数尝试都产生了一个空白的.csv或我实际代码的可爱.csv!
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT CLIENT.company,CLIENT.first,CLIENT.last,INVOICES.clientid,CLIENT.class_id,INVOICES.invid,FROM_UNIXTIME(INVOICES.date)as date,INVOICES.amount,payment_records.type,inv_taxes.name,inv_taxes.value,inv_taxes.rate
FROM `INVOICES`
LEFT JOIN payment_records ON payment_records.invid = INVOICES.invid
LEFT JOIN inv_taxes ON inv_taxes.invid = INVOICES.invid
RIGHT JOIN CLIENT ON CLIENT.clientid = payment_records.clientid
where CLIENT.class_id = :Brand_Id and INVOICES.date >= :from_date && INVOICES.date <= :to_date GROUP BY INVOICES.invid");
$stmt->bindParam(":Brand_Id", ($_POST['Brand_Id']));
$stmt->bindParam(":from_date", ($_POST['from_date']));
$stmt->bindParam(":to_date", ($_POST['to_date']));
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
<!-- This is my script which does display the datepicker when the input boxes are clicked -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Taxes </title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$.datepicker.setDefaults({
showOn: "button",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
buttonText: "Date Picker",
buttonImageOnly: true,
dateFormat: '@',
altTimeFormat: 'HH mm ss'
});
$(function() {
$("#from_date").datepicker({ dateFormat: '@' })
.change( function(){ this.value = parseInt(this.value,10)/1000; });
$("#to_date").datepicker({ dateFormat: '@' })
.change( function(){ this.value = parseInt(this.value,10)/1000; });
});
</script>
<!--{ dateFormat : '@'}-->
</body>
</html>
发布于 2020-04-09 08:10:08
您可以将结果存储在数组中,并使用fputcsv将其放入csv中。
https://stackoverflow.com/questions/61116437
复制