I'm using following solution to convert HTML tables to CSV: Converting HTML Table to a CSV automatically using PHP? and it works fine when I use two files alone in my server.
Here you can see the adaptation of my code:
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>';
?>
File 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);
?>
As I said above these files together give me the result that I expect: a beautiful CSV file. (: The problem occurs when I try to adapt it to MODx Revolution. I'm doing just like that:
In MODx Revolution I have a Resource called Result Table which has a snippet [[resultTable]] and a Link to the file html2csv.php. Here the source of the Resource:
<p>[[!resultTable]]</p>
<p><a href="http://example.com/html2csv.php">Export to CSV</a></p>
And here the snippet 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;
?>
The only essential modification is on the last lines where I didn't use echo, but return, just as advised by MODx Tutorial. Until here it works as well and I'm able to see the result table. When I click on the link that loads the file html2csv.php I am able to download a csv file, but unfortunately it has following error message:
<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 />
I've tried some workaround, but none has worked. So I hope that someone out there can help me once again and explain me what I'm doing wrong. (:
Many thanks in advance!