我有一个PHP脚本,将我的搜索输出到excel。它没有正确的格式化,它包括我的网站和我的搜索栏中的菜单。
如何将其带到不输出菜单或搜索栏?或者我应该将数据导出到csv?我不确定更容易......
以下是我当前的工作代码:
normalsearch.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>
export.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';
?>
答案 0 :(得分:0)
在这里,我可以为您提供一个用于创建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();
}
所以请这样试试。