我正在使用以下解决方案将HTML表转换为CSV:使用PHP自动将HTML表转换为CSV ?当我在服务器上单独使用两个文件时,它可以正常工作。
在这里你可以看到我的代码的改编:
File 01: TABLE.PHP
<?php
session_start();
$query_select = 'SELECT id, name, department, year FROM table WHERE year = 2000 ORDER BY name DESC';
$query_result = mysqli_query($link, $query_select) or die ("Couldn't execute query: ".mysqli_error());
$table = '
<table id="result-table" class="table table-bordered table-hover">
<thead>
<tr>
<td>ID</td>
<td>NAME</td>
<td>DEPARTMENT</td>
<td>YEAR</td>
<td>Check</td>
</tr>
</thead>';
while($row = mysqli_fetch_array($query_result)) {
$table .= "
<tbody>
<tr>
"<td>".$row['id']."</td>".
"<td>".$row['name']."</td>".
"<td>".$row['department']."</td>".
"<td>".$row['year']."</td>".
"<td><input type='checkbox' name=".$row['id']." value=".$row['id']."></td>".
</tr>";
}
$table .= '
</tbody>
</table>';
$_SESSION['table'] = $table;
echo $table;
echo '<a href=html2csv.php>Export to CSV</a>';
?>
文件02:HTML2CSV.php
<?php
session_start();
include 'simple_html_dom.php'; // Which is in http://www.example.com/simple_html.php as well.
$table = $_SESSION['table'];
$html = str_get_html($table);
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename=file.csv');
$fp = fopen("php://output", "w");
foreach($html->find('tr') as $element) {
$td = array();
foreach($element->find('th') as $row) {
$td [] = $row->plaintext;
}
fputcsv($fp, $td);
$td = array();
foreach( $element->find('td') as $row) {
$td [] = $row->plaintext;
}
fputcsv($fp, $td);
}
fclose($fp);
?>
正如我上面所说的,这些文件一起给了我我所期望的结果:一个漂亮的CSV文件。(:问题发生时,我试图适应它的MODx革命。我就是这样做的:
在MODx革命我有一个叫做结果表的资源,它有一个片段[[resultTable]]和一个链接到文件html2csv.php。这里是资源的来源:
<p>[[!resultTable]]</p>
<p><a href="http://example.com/html2csv.php">Export to CSV</a></p>
这里的代码片段resultTable:
<?php
$query_select = 'SELECT id, name, department, year FROM table WHERE year = 2000 ORDER BY name DESC';
$query_result = mysqli_query($link, $query_select) or die ("Couldn't execute query: ".mysqli_error());
$table = '
<table id="result-table" class="table table-bordered table-hover">
<thead>
<tr>
<td>ID</td>
<td>NAME</td>
<td>DEPARTMENT</td>
<td>YEAR</td>
<td>Check</td>
</tr>
</thead>';
while($row = mysqli_fetch_array($query_result)) {
$table .= "
<tbody>
<tr>
"<td>".$row['id']."</td>".
"<td>".$row['name']."</td>".
"<td>".$row['department']."</td>".
"<td>".$row['year']."</td>".
"<td><input type='checkbox' name=".$row['id']." value=".$row['id']."></td>".
</tr>";
}
$table .= '
</tbody>
</table>';
$_SESSION['table'] = $table;
return $table;
?>
唯一必要的修改是在最后一行,我没有使用echo,而是返回,正如MODx教程所建议的那样。直到这里,它的工作以及,我能够看到结果表。当我点击加载文件html2csv.php的链接时,我能够下载一个csv文件,但不幸的是它有以下错误信息:
<b>Fatal error</b>: Call to a member function find() on a non-object in <b>/home/aloysia/www/html2csv.php</b> on line <b>13</b><br />
我已经尝试了一些变通方法,但都不起作用。所以我希望有人能再次帮助我,告诉我我做错了什么。(:
提前感谢!
问题可能是您的$_SESSION['table']
没有设置。当modx运行时,它启动它自己的会话,你不能从外部自动访问。您可以在html2csv.php中通过var_dump($_SESSION)
快速测试它。我不太确定你的解决方案,为什么你不直接从数据库生成CSV ?但是,如果你不能改变解决方案,我会尝试包括modx的index.php和设置MODX_API_MODE = true
访问功能,而不加载请求。(假设你在同一个web根的某个地方!)
//In html2csv.php
define('MODX_API_MODE', true);
require_once('..path/to/index.php');
//You should now be able to access $_SESSION['table']
var_dump($_SESSION['table']);