1

i have this error:

Error Trying to get property of non-object

and the code is:

 $dom->preserveWhiteSpace = false; 

    $tables = $dom->getElementsByTagName('table'); 

    $rows = $tables->item(0)->getElementsByTagName('tr'); 

   $i=0; 
   foreach ($rows as $row) 
    { 
        /*** get each column by tag name ***/ 
        $cols = $row->getElementsByTagName('td'); 

        $this->data['Table'][$i] = array(
                           'Attrb1' => $cols->item(0)->nodeValue,
                           'Attrib2' => $cols->item(1)->nodeValue

                                  );
     $i++;

    } 
         }

        return $this->toArray();
    }

The line that i have this error is :

'Attrib2' => $cols->item(1)->nodeValue

the html code is:

<table border=1 align="center" cellpadding=5 width="95%">
                                            <!doctype html public "-//w3c//dtd html 4.0//EN">
<html it>
<head>
<meta name="Generator" content="OLS">
</head>
<body>
<td colspan=2 align="center">
<b>
<i>
Attrib1
</i>
</b>
</td>
<td>
<b>
<i>
Attrib2
</i>
</b>
</td>
<td>
<b>
<i>
<tr>
<td>
A000211
</td>
<td nowrap>
Statistic
</td>
</tr>

but i don't know if the problem is in the foreach or all get data that i make is one big error or i process the html in a wrong way..so help me please..

3
  • The error suggests that there are not two TDs in that row. - Which can make sense as a quick scan of the HMTL shows some irregularities that might be corrected when loaded. - Please isolate your issue into an example that you create from scratch containing as little data and code as necessary to demonstrate your issue. Also I suggest to create a HTML table model to deal with tables generally so that you can more easily access rows and columns. Commented Oct 27, 2013 at 9:33
  • not the TDs are 8 but still if i try to put all i have this error..and i can't change the html. Commented Oct 27, 2013 at 9:52
  • a example of row is: <td> 0065207 </td> <td nowrap> Telematic </td> <td align="center"> 18 </td> <td align="center"> 6 </td> <td align="center"> 04/09/2012 </td> <td align="center"> 2011/2012 </td> <td> Check </td> <td align="center"> 2008/2009 </td> </tr> Commented Oct 27, 2013 at 10:02

1 Answer 1

3

You can use:

$dom->loadHTML($result); 
libxml_clear_errors();
libxml_use_internal_errors($errors);
/*** discard white space ***/ 
$dom->preserveWhiteSpace = false; 
/*** the table by its tag name ***/ 
$tables = $dom->getElementsByTagName('table'); 
/*** get all rows from the table ***/ 
$rows = $tables->item(1)->getElementsByTagName('tr'); 
/*** loop over the table rows ***/ 
$i=0; 
foreach ($rows as $row) { 
    /*** get each column by tag name ***/ 
    $cols = $row->getElementsByTagName('td'); 

    $this->data['List'][$i] = array(
       'name1' => $cols->item(1)->nodeValue,
       'name2' => $cols->item(2)->nodeValue,
       'name3' => $cols->item(3)->nodeValue,
       'name4' => $cols->item(4)->nodeValue,
       'name5' => $cols->item(5)->nodeValue,
       'name6' => $cols->item(6)->nodeValue,
       'name7' => $cols->item(7)->nodeValue
    );
    $i++;
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.