简体   繁体   English

PHP Excel导出仅显示MYsql表的最后一行

[英]PHP Excel export only shows last row of MYsql table

I am having problems exporting data from a MySQL database using a PHP script that mimes an excel file. 我在使用模仿Excel文件的PHP脚本从MySQL数据库导出数据时遇到问题。 The data is only exporting the last row of the MySQL table. 数据仅导出MySQL表的最后一行。 I have chopped the code down to remove all the relational look ups (as there are multiple MySQL queries through out which make it hard to read). 我将代码砍掉了,以删除所有的关系查找(因为存在多个MySQL查询,因此很难阅读)。 I understand I am over writing my variables so only the last row of selected is available to the script but after a lot of searching I still cant seem to find an answer (I am guessing I need to store the data in an array then call that array within the code that exports the data as an excel file). 我知道我已经写完了变量,因此脚本只可以选择最后一行,但是经过大量搜索之后,我仍然似乎找不到答案(我想我需要将数据存储在数组中,然后调用该变量将数据导出为ex​​cel文件的代码中的数组)。 All help will be greatly appreciated. 所有帮助将不胜感激。 My code (chopped down version) is: 我的代码(缩减版本)为:

<?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;
?>

Many thanks in advance. 提前谢谢了。

Cheers Jase 干杯

PS I worked this code up by searching the web over the last few days and put it together from that prior to this I have never worked on this type of stuff (outputting file types) 附言:我通过在最近几天的网上搜索来整理这段代码,并将其与之前的内容放在一起,我从未从事过此类工作(输出文件类型)

Your code could use a lot of cleanup, but I will let you figure that out later, and focus on making it work as you have intended. 您的代码可能需要进行大量清理,但是稍后我将让您弄清楚这一点,并集中精力使其按预期工作。

You can do this by using concatenation .= 您可以使用串联.=来完成此操作

//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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM