gpt4 book ai didi

php - 将搜索结果输出到 Excel 或 CSV

转载 作者:行者123 更新时间:2023-11-29 19:35:12 25 4
gpt4 key购买 nike

我有一个 php 脚本,它将我的搜索输出到 Excel 中。它的格式不正确,它包括我网站上的菜单和我的搜索栏。

如何让它不输出菜单或搜索栏?或者我应该将数据导出到 csv 中?我不确定什么更容易..

这是我当前的工作代码:

正常搜索.php:

<html>
<header>

<?php include 'menu2.php'; ?>
<br>
<form action="normalsearch.php" method="GET">
LF. Nr / Code :
<input type="text" name="query" />
<input type="submit" value="Suchen" />
</form>

<form action="export.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Excel">
</form>

</header>

<br>
<br>

<body>
<table border="1">
<tr>
<th>Number</th>
<th>Product ID</th>
<th>Description</th>
<th>Stock</th>
</tr>

<?php
$query = $_GET['query'];

//connection to mysql
mysql_connect("host", "user", "password"); //server , username , password
mysql_select_db("database");

//query get data
$sql = mysql_query("SELECT * FROM database WHERE productID LIKE '%".$query."%' LIMIT 100");
$no = 1;
while($data = mysql_fetch_assoc($sql)){
echo '
<tr>
<td>'.$no.'</td>
<td>'.$data['productID'].'</td>
<td>'.$data['description'].'</td>
<td>'.$data['stock'].'</td>
</tr>
';
$no++;
}
?>
</body>
</html>

导出.php:

<?php
// The function header by sending raw excel
header("Content-type: application/vnd.ms-excel");
// Defines the name of the export file "codelution-export.xls"
header("Content-Disposition: attachment; filename=Artikelsuche.xls");
// Add data table
include 'normalsearch.php';
?>

最佳答案

在这里,我可以为您提供用于创建 CSV 文件的示例 PHP 代码。 我强烈推荐你 - 不要使用mysql使用 PDO 或 mysqli。我不会编写完整的代码。因为新开发者/学习者只是从堆栈溢出或其他一些博客中复制。他们自己并没有尝试。去年,开发人员/程序员/工程师得到了另一个标题。

开发人员/程序员/工程师 - 从 StackOverflow 复制粘贴作业。

这太荒谬了。很遗憾地说这些事情。

$filename = 'Your filename'; //You can include your filename with the date or other data.

//Suppose you want to show some product data

$csvdata="ProductId,ProductName,Category,Subcategory,Price\n";

header("Content-Disposition: attachment;filename=".$filename.".csv");

print $csvdata;


//Now you can retrieve data from the database. I hope, you will have a common DB connection function.

$Databaseconnection = DBConnection();

//Your conditions

//Your Query

if(!$result=$Databaseconnection->query($query))
{
error_log("SQL error ");
return;
}

if($result->num_rows==0)
{
error_log("Your Message");
return;
}



while($row=$result->fetch_assoc())
{

$csvdata=implode(",",array_values($row));
$csvdata.="\n";
print $csvdata;
flush();
ob_flush();
}

所以请尝试这种方式。

关于php - 将搜索结果输出到 Excel 或 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41587445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com