[英]PHP export to CSV - Doesnt Export newest Data
I have spent the past 5 or so hours searching forums and the internet for an answer to this issue. 在过去的5个小时左右的时间里,我一直在论坛和互联网上搜索该问题的答案。
Basically, I have a webpage where a table is generated using data collected from the Oracle SQL Server. 基本上,我有一个网页,其中使用从Oracle SQL Server收集的数据生成表。
When the page loads, the table shows with the past 7 days worth of data. 页面加载后,该表将显示过去7天的数据。 I have a date selection field with a "Go" button and an "Export to CSV" button.
我有一个带有“转到”按钮和“导出为CSV”按钮的日期选择字段。
If I click the Export button it prompts be to save or open. 如果单击“导出”按钮,则提示是保存或打开。 The CSV is fine it has the correct Data etc etc.
CSV很好,它具有正确的数据等。
The issue is, when I select a date using the date selection field and hit go... the Table on the page refreshes showing me the data for the specific day. 问题是,当我使用日期选择字段选择一个日期并单击go ...时,页面上的表将刷新,为我显示特定日期的数据。 If I then click Export to CSV, the data seems to still be the original Data from when the page first loaded.
如果然后单击“导出为CSV”,则该数据似乎仍是页面首次加载时的原始数据。
This is my code... I am sorry if the code isn't very great I am a complete novice to php. 这是我的代码...对不起,如果代码不是很好,我是php的新手。
if(isset($_POST['csv'])) $csv_request=$_POST['csv']; else $csv_request="";
if(isset($_POST['datepicker'])) $date_filter=$_POST['datepicker']; else $date_filter="";
$output_csv = "";
$chosenDate = $date_filter;
if($chosenDate== "" || $chosenDate == Null){
$mywhere = 'where timestamp >= trunc(sysdate)-7';
}
elseif ($chosenDate=='datepicker'){
$date_chosen = $_POST['datePicker'];
$mywhere = "where timestamp like to_date('$date_chosen', 'dd/mm/yyyy')";
}
$sql = "Select to_char(timestamp, 'dd/mm/yy hh24:mi') timestamp, site, sum(Manual_coos) Manual_COOS,Sum(Faulty_COOS) Faulty_COOS, bearer from t_ca_Processed
".$mywhere."
AND bearer = '4G'
AND faulty_coos = 1
group by timestamp, site, Manual_coos, Faulty_COOS, bearer
UNION ALL
Select to_char(timestamp, 'dd/mm/yy hh24:mi') timestamp, site, sum(Manual_coos) Manual_COOS,Sum(Faulty_COOS) Faulty_COOS, bearer from t_ca_Processed
".$mywhere."
AND bearer = '4G'
AND Manual_coos = 1
group by timestamp, site, Manual_coos, Faulty_COOS, bearer
Order by timestamp ASC";
$st = $db->execute($sql);
$output = "<table class = 'sortable' border = 1 Align = 'Center'>
<tr style = 'background-color: #f00;'>
<th>TimeStamp</th>
<th>Site</th>
<th>Manual COOS Total</th>
<th>Faulty COOS Total</th>
<th>Bearer</th>
</tr>";
$output_csv = "TimeStamp,Site,Manual COOS Total,Faulty COOS Total,Bearer\n";
$counter = 0;
while (($row = oci_fetch_row($st)) != false)
{
$output .= "<tr";
$output .= "><td>$row[0]</td>
<td align=center>$row[1]</td>
<td align=center>$row[2]</td>
<td align=center>$row[3]</td>
<td align=center>$row[4]</td></tr>\n";
$output_csv .= "$row[0],$row[1],$row[2],$row[3],$row[4]\n";
$counter ++;
}
$output .= "</table>";
unset($db);
If ($csv_request=='csv'){
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$File_name.".csv\";");
header("Content-Transfer-Encoding: binary");
echo $output_csv;
}
else
{
This is my HTML 这是我的HTML
<!DOCTYPE html>
<html>
<head>
<style>
table.sortable tbody tr:nth-child(2n) td {
background: #DDDDDD;
}
table.sortable tbody tr:nth-child(2n+1) td {
background: #ccfffff;
}
</style>
<script type='text/javascript' src="/common/javascript/kpc_sorttable2.js"></script>
<script type='text/javascript' src='/common/flot/jquery.js'></script>
<script type="text/javascript" src="/common/jquery/ui/1.10.2/ui/jquery-ui.js"></script>
<script type="text/javascript" src="/Common/jQuery/ui/1.10.2/ui/jquery.ui.core.js"></script>
<script type="text/javascript" src="/Common/jQuery/ui/1.10.2/ui/jquery.ui.widget.js"></script>
<script type="text/javascript" src="/Common/jQuery/ui/1.10.2/ui/jquery.ui.datepicker.js"></script>
<link rel= 'stylesheet' type=text/css href='/common/style.css'>
<script>
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: 'dd/mm/yy'
})
})
</script>
<script>
</script>
</head>
<body>
<?php generateHeader($menu,'4G Cells Down',1); ?>
<div class='whitebox'>
<form method='post' action="" name="parameters" id="parameters">
<input type='hidden' name='csv' value='csv'>
<input type='submit' value="Export as CSV" style="float: right;">
</form>
<form method='post' action="" name="parameters1" id="parameters1">
<h3>Last Refreshed <?= $page_load_time ?></h3>
<input type='hidden' name='datepicker' value='datepicker'>
<h3>Date: <input type="text" id="datepicker" name="datePicker"><input type='submit' value="Go"></h3>
<p><?= $output ?><p>
</form>
</div>
<?php
generateFooter();
?>
</body>
</html>
<?php } ?>
when I echo $output_csv I always see the newest data set... however the CSV is always the original Data. 当我回显$ output_csv时,我总是看到最新的数据集...但是CSV始终是原始数据。 I have tried to Echo the CSV and I get an error saying headers have already been sent...
我尝试回显CSV,但收到一条错误消息,指出标题已发送。
I am wondering if when the page originally loads, the csv has been created and only gets prompted to download once I hit the export button and when I have the new data, the file cannot be re-written? 我想知道是否在最初加载页面时创建了csv,并且仅在单击“导出”按钮后才得到下载提示,并且当我拥有新数据时,是否无法重写该文件?
Again sorry if im not very clear, new to this :) 再次抱歉,如果即时消息不是很清楚,这是新的:)
cheers Mike 迈克欢呼
Your file probably gets cached by the webserver. 您的文件可能已被Web服务器缓存。 If you add a Cache buster and/or cache header it should work.
如果添加了缓存无效化器和/或缓存头,则它应该可以工作。
header("Content-Transfer-Encoding: binary");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
More info about the PHP header function, including caching directives. 有关PHP 标头功能的更多信息,包括缓存指令。
If you generate a unique filename for each change or request, it will also be seen as a new file and caching will be disabled. 如果您为每个更改或请求生成一个唯一的文件名,它也将被视为一个新文件,并且将禁用缓存。
# Generate a unique name for each request
$File_name = uniqid($File_name . "_");
header("Content-Disposition: attachment; filename=\"".$File_name.".csv\";");
See PHP function uniqid for more specifics about the functionality. 有关功能的更多详细信息,请参见PHP函数uniqid 。
The problem is that when you search for a date, the page refresh and the form refresh too, so when you click on the export csv the "request" is to get the default view. 问题在于,当您搜索日期时,页面刷新和表单也会刷新,因此,当您单击导出csv时,“请求”将获得默认视图。 Keep your form (selected data) updated with the current view.
使用当前视图保持表单(选定数据)的更新。
The value of this 这个的价值
<input type="text" id="datepicker" name="datePicker">
have to been keep always with the current view 必须始终保持当前视图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.