0

I get this error: Fatal error: Call to a member function getElementsByTagName() on a non-object in /XXX on line 17

This is my PHP:

$html   = file_get_contents('../test.html');
$doc    = new DOMDocument();
$doc->loadHTML($html);

$titles = array();
$table  = $doc->getElementById('my_table');
$a      = 0;
$delim  = "";
$csv    = "";

//number of columns in the table
$num_cols   = 4;

foreach($table->getElementsByTagName('td') as $td) {
    if($a == 0) {
        $delim = "";
    }

    if($a < $num_cols) {
        $csv   .= $delim . '"' . $td->nodeValue. '"';
        $delim  = ";";
    }

    if($a == ($num_cols-1)) {
        $csv   .= "\n";
        $a      = -1;
    }
    $a++;
}

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=wm_export.csv");
echo $csv;
exit;
?>

And this my HTML file ../text.html

 <table id="my_table" style="width: 600px;">
<tbody>
<tr>
    <td>Name</td>
    <td>Surname</td>
    <td>Address</td>
    <td>City</td>
</tr>
<tr>
    <td>Alfred</td>
    <td>William</td>
    <td>327 Cranbrook Road</td>
    <td>London</td>
</tr>
<tr>
    <td>Mario</td>
    <td>Rossi</td>
    <td>via Genova</td>
    <td>Milano</td>
</tr>
<tr>
    <td>Eddy</td>
    <td>Smitt</td>
    <td>Cranbrook Road</td>
    <td>Toronto</td>
</tr>
</tbody>
</table>
kelunik
  • 6,520
  • 2
  • 38
  • 69
zerophreak
  • 294
  • 1
  • 4
  • 11
  • 1
    "test.html" vs "text.html" – Ja͢ck May 02 '14 at 15:43
  • Have you tried `var_dump($html);` to make sure it opened the contents correctly? It seems like it isn't finding your table. – Sam May 02 '14 at 15:43
  • `$table` is probably `null`. – kelunik May 02 '14 at 15:44
  • Sorry, text.html is my mistake, yes $table is null – zerophreak May 02 '14 at 15:44
  • var_dump does work :: string(556) " Name Surname Address City Alfred William 327 Cranbrook Road London Mario Rossi via Genova Milano Eddy Smitt Cranbrook Road Toronto" – zerophreak May 02 '14 at 15:45
  • 2
    Works fine on my machine; it produces CSV. Also, use `fputcsv()`. – Ja͢ck May 02 '14 at 15:47
  • Does the `var_dump()` output not contain the HTML? It looks like a normal string, without the table. – Sam May 02 '14 at 15:48
  • Do you think it's a server problem? @Sam It does contain, I just pasted the output here... (without html) – zerophreak May 02 '14 at 15:48
  • Same here, changed 'test.html' to 'text.html', copy 'n' pasted your code/markup, and ran. Works fine. – Darragh Enright May 02 '14 at 15:48
  • Is that your entire HTML file? Invalid contents before/after it could be giving `DOMDocument` difficulties.. – Sam May 02 '14 at 15:51
  • What can be my problem then? :( @Sam yes just table – zerophreak May 02 '14 at 15:52
  • My $doc is empty: object(DOMDocument)#1 (0) { } May it be PHP doesn't have DOMDocument installed – zerophreak May 02 '14 at 15:54
  • You would get an error saying `DOMDocument` doesn't exist. [Enable all error reporting](http://stackoverflow.com/questions/6575482/how-do-i-enable-error-reporting-in-php) and see what kind of warning gets thrown when you do `->loadHTML()`..I bet you have some invalid HTML not shown in `text.html` that is causing problems. – Sam May 02 '14 at 15:56
  • If it didn't have DOMDocument installed, `new DOMDocument` would fail and you wouldn't get an DOMDocument object. – Barmar May 02 '14 at 15:56
  • @Sam I only have the upper HTML code! Seriously! – zerophreak May 02 '14 at 15:57
  • Haha, okay. Still, I bet your error reporting only shows fatal errors. Use `error_reporting(-1);` to see if a warning shows up that can give us more info. – Sam May 02 '14 at 15:58
  • I have ini_set("display_errors", "on"); activated, - error_reporting() doesn't work with the server configuration – zerophreak May 02 '14 at 15:59

0 Answers0