douyou7797 2014-05-12 20:49
浏览 25

在MODx Revolution中尝试将HTML表转换为CSV的问题

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!

  • 写回答

1条回答 默认 最新

  • duai4379 2014-05-13 06:32
    关注

    The problem is probably your $_SESSION['table'] not being set. When modx runs, it initiates it's own session which you can't access automatically from outside. You could test it quickly by var_dump($_SESSION) in your html2csv.php. I'm not really sure about your solution overall, and why you don't generate the CSV directly from the database? But if you can't change the solution i would try including modx's index.php and setting MODX_API_MODE = true to access functions without loading a request. (assuming you're in the same web root somewhere!)

    //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']);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集