【发布时间】:2017-01-11 09:47:47
【问题描述】:
我有一个 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';
?>
【问题讨论】:
-
每次在新代码中使用the
mysql_数据库扩展a Kitten is strangled somewhere in the world 时,它都已被弃用,并且已经存在多年,并且在 PHP7 中永远消失了。如果您只是学习 PHP,请花精力学习PDO或mysqli数据库扩展和预处理语句。 Start here -
那么尝试输出一个格式为逗号分隔的字符串,而不是整个网页
-
因为这基本上需要完全重写,所以我投票决定关闭它,因为它太宽了