[英]PHP Excel export only shows last row of MYsql table
我在使用模仿Excel文件的PHP腳本從MySQL數據庫導出數據時遇到問題。 數據僅導出MySQL表的最后一行。 我將代碼砍掉了,以刪除所有的關系查找(因為存在多個MySQL查詢,因此很難閱讀)。 我知道我已經寫完了變量,因此腳本只可以選擇最后一行,但是經過大量搜索之后,我仍然似乎找不到答案(我想我需要將數據存儲在數組中,然后調用該變量將數據導出為excel文件的代碼中的數組)。 所有幫助將不勝感激。 我的代碼(縮減版本)為:
<?php
// Apply server security settings and keep cookies
// connecting to the server
// selecting the appropriate database
//storing and fetching user data
$generate_query = "SELECT * FROM main_report";
$generate_data = mysql_query($generate_query, $link);
while($report = mysql_fetch_array($generate_data))
{
$reportnoout = "<td>".$report['report_number']."</td>";
$incdateout = "<td>".$report['incident_time']."</td>";
$siteout = "<td>".$site_data['site_name']."</td>";
$deptout = "<td>".$dept_data['department_name']."</td>";
$reportout = " <td>".$report['report_type']."</td>";
$superout = "<td>".$staff_data5['name']."</td>";
$descout = "<td>".$report['detailed_desc']."</td>";
// Needs some form of array declaration here maybe?
}
// filename for download
$filename = "test_data_" . date('Ymd') . ".xls";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");
$test="<table><th>Report No.</th><th>Incident Date</th><th>Site</th><th>Department</th><th>Incident Type</th><th>Responsible Supervisor</th><th>Description</th><tr>";
$test2="$reportnoout $incdateout $siteout $deptout $reportout $superout $descout"; // This is not right either should probably be an array or not even here?
echo $test;
echo $test2; // This was a stop gap to at least see if some of the code worked
exit;
?>
提前謝謝了。
干杯
附言:我通過在最近幾天的網上搜索來整理這段代碼,並將其與之前的內容放在一起,我從未從事過此類工作(輸出文件類型)
您的代碼可能需要進行大量清理,但是稍后我將讓您弄清楚這一點,並集中精力使其按預期工作。
您可以使用串聯.=
來完成此操作
//start table string
$table = "<table><tr>
<th>Report No.</th>
<th>Incident Date</th>
<th>Site</th>
<th>Department</th>
<th>Incident Type</th>
<th>Responsible Supervisor</th>
<th>Description</th><tr>";
$generate_query = "SELECT * FROM main_report";
$generate_data = mysql_query($generate_query, $link);
while($report = mysql_fetch_array($generate_data))
{
//add row to string using concatenation
$table .= "<tr><td>{$report['report_number']}</td>
<td>{$report['incident_time']}</td>
<td>{$site_data['site_name']}</td>
<td>{$dept_data['department_name']}</td>
<td>{$report['report_type']}</td>
<td>{$staff_data5['name']}</td>
<td>{$report['detailed_desc']}</td></tr>";
}
//close table
$table .="</table>";
// filename for download
$filename = "test_data_" . date('Ymd') . ".xls";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");
echo $table;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.