【问题标题】:Output results of a search into Excel or CSV将搜索结果输出到 Excel 或 CSV
【发布时间】: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,请花精力学习 PDOmysqli 数据库扩展和预处理语句。 Start here
  • 那么尝试输出一个格式为逗号分隔的字符串,而不是整个网页
  • 因为这基本上需要完全重写,所以我投票决定关闭它,因为它太宽了

标签: php mysql excel csv


【解决方案1】:

这里我可以给你一个用于创建 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();
    }

所以请尝试这种方式。

【讨论】:

    猜你喜欢
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    相关资源
    最近更新 更多