[英]PHP export mysql data to excel sheet
I have a filter that the user selects or inputs information, and when they hit submit, a query is generated with this code: 我有一个过滤器,用户可以选择或输入信息,当他们单击“提交”时,将使用以下代码生成查询:
<?php
function displayrecords(){
$data1 = $_POST['data1'];
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$sql_json = "";
$where = "";
if($data1 != ""){
$where = " `data1` = '".mysql_real_escape_string($data1)."'";
}
if($data2 != ""){
if ( $where != "" ) $where .= " AND ";
$where = " `data2` = '".mysql_real_escape_string($data2)."'";
}
if($data3 != ""){
if ( $where != "" ) $where .= " AND ";
$where = " `data3` = '".mysql_real_escape_string($data3)."'";
}
if ($where != "") $sql_json = "SELECT * FROM `database`" . " WHERE " . $where . ";";
$QueryResult = @mysql_query($sql_json) or die (mysql_error());
echo "<table>"
echo "<tr><th>Data1</th>" . "<th>Data2</th>" . "<th>Data3</th></tr>\n";
while(($Row = mysql_fetch_assoc($QueryResult)) !== FALSE) {
echo "<tr><td>{$Row['data1']}</td>";
echo "<td>{$Row['data2']}</td>";
echo "<td>{$Row['data3']}</td></tr>\n";
};
echo "</table>"\n";
}
}
It then spits the data out with this: 然后,它使用以下方法吐出数据:
<?php displayrecords(); ?>
There are no issues here. 这里没有问题。 The query builder works.
查询生成器有效。 The data is rendered to the screen in the table.
数据将呈现到表中的屏幕。
What I need to do now is take the data that has been returned to the screen and export it to an excel sheet using another button in the same form as the other submit button. 我现在需要做的是使用已返回到屏幕的数据,并使用与另一个提交按钮相同形式的另一个按钮将其导出到Excel工作表。 Here is the button that is in the same form:
这是具有相同形式的按钮:
<input class="btn btn-success btn-small" type="submit" name="get_report" value="Get Report" />
I don't know if I can use another submit button in the same form. 我不知道我是否可以使用相同形式的另一个提交按钮。 I think on have to use a javascript onclick() function.
我认为必须使用javascript onclick()函数。 I'm not sure.
我不确定。 I just need to export the returned data to excel.
我只需要将返回的数据导出到excel。
Can anyone help? 有人可以帮忙吗?
A few things to consider: 需要考虑的几件事:
Don't use $_POST[]
within a function. 不要在函数内使用
$_POST[]
。 It limits the function to only dealing with POST. 它将功能限制为仅处理POST。 Rather, call the function with your POST data.
而是使用您的POST数据调用该函数。
Have the data retrieved from the database and returned in its own function, something like getTableData()
, so that your HTML and your spreadsheet scripts will be able to query the data independently without redundant code. 从数据库中检索数据并以其自己的函数(如
getTableData()
,这样您的HTML和电子表格脚本将能够独立地查询数据而无需冗余代码。
Also, it is always recommended to use mysqli_
or PDO over mysql_*
, as mysql_
is deprecated. 此外,它总是建议使用
mysqli_
或PDO在mysql_*
,如mysql_
已被弃用。
I will not address the database query or the html table display in this answer, as it seems you are quite capable of handling that aspect of the task on your own. 在此答案中,我将不解决数据库查询或html表显示问题,因为您似乎完全有能力自行处理任务的这一方面。
With all that in mind, my method would be the following: 考虑到所有这些,我的方法如下:
At the end of the HTML table, place a link to a csv file in a subdirectory like so: 在HTML表的末尾,将指向csv文件的链接放在子目录中,如下所示:
<a href='download/some.csv?<?php echo 'data1='.urlencode($_POST['data1'], ENT_QUOTES).'&data2='.urlencode($_POST['data2'], ENT_QUOTES).'&data3='.urlencode($_POST['data3'], ENT_QUOTES).; ?>'>Download CSV</a>
Then, in your download
folder, you'll need a .htaccess
file with the following contents: 然后,在
download
文件夹中,您将需要一个具有以下内容的.htaccess
文件:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ getFile.php?file=$0 [QSA,L]
Also in your download
folder, you'll place a getFile.php
file that may look something like this: 同样在您的
download
文件夹中,您将放置一个看起来像这样的getFile.php
文件:
<?php
$fileName = (isset($_GET['file'])) ? $_GET['file'] : false;
$data1 = (isset($_GET['data1'])) ? $_GET['data1'] : false;
$data2 = (isset($_GET['data2'])) ? $_GET['data2'] : false;
$data3 = (isset($_GET['data3'])) ? $_GET['data3'] : false;
if($fileName && $data = getTableData($data1, $data2, $data3)) //This would be calling your new function designed specifically to retrieve your data in a multidimensional array
{
header("HTTP/1.0 200 OK");
header("Content-Type: text/csv");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
foreach($data as $row)
{
foreach($row as $columnCount=>$field)
{
echo ($columnCount) ? ','.csvField($field) : csvField($field);
}
echo "\r\n";
}
}
else header("HTTP/1.0 404 Not Found");
function csvField($value, $textDelimiter = '"')
{
if (strstr($value, $textDelimiter) || strstr($value, ",")) $value = $textDelimiter.str_replace($textDelimiter, $textDelimiter.$textDelimiter, $value).$textDelimiter;
return $value;
}
?>
the .htaccess
file passes the file name to your getFile.php
script in a $_GET
parameter. .htaccess
文件将文件名通过$_GET
参数传递给getFile.php
脚本。 Your .csv file must not actually exist , so that .htaccess
can redirect the request to getFile.php
. 您的.csv文件必须实际上不存在 ,以便
.htaccess
可以将请求重定向到getFile.php
。
Make sure that in your getTableData()
function the variables passed are properly protected against SQL injection. 确保在
getTableData()
函数中传递的变量受到适当的保护,以防止SQL注入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.