Link to home
Start Free TrialLog in
Avatar of Member_2_5230414
Member_2_5230414

asked on

php sime html dom error Call to a member function find() on this line foreach($table->find('tr') as $article) {

What am i doing wrong here???




<?
include '../simple_html_dom.php';
$html = new simple_html_dom();

$url = ''.$id;



$postdata = array('login' => "",
		          'password' => "");

$ch = curl_init();
if($ch){
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
   curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // set cookie file to given file
   curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // set same file as cookie jar

   $content = curl_exec($ch);
   $headers = curl_getinfo($ch);				

   curl_close($ch);
	
	
	
	
	$dom = new DOMDocument();
  // load html
  @$dom->loadHTML($content);
  $xpath = new DOMXPath($dom);

  //this will gives you all td with class name is jobs.
  $my_xpath_query = '//table/tr/td[contains(@class, "databreakdown")]';
  $result_rows = $xpath->query($my_xpath_query);
  // First check if we found anything
  if ($result_rows->length > 0) {
    // IF WE DID GET THE PARENT'S <tr> PARENT'S <table> NODE
    $obj = $result_rows->item(0)->parentNode->parentNode;
    // CALL saveHTML ON THE TABLE OBJ TO SAVE TO STRING
    $table = $dom->saveHTML($obj);
   
	  foreach($table->find('tr') as $article) {
		  
		   
				
    $item['horse']     =$thetr->find('td', 2)->plaintext;
    $item['place']    = $thetr->find('td', 2)->plaintext;
    $item['odds'] = $thetr->find('td', 3)->plaintext;
    $thetr[] = $item;
				
		
		  
}
	 print_r($thetr); 
	  
  }
  else {
    echo "Table not found";
  }
	
	
   
}
?>

Open in new window


I get the error Call to a member function find() on this line foreach($table->find('tr') as $article) {

im trying to go through the table and pick out each tr and display the data (what i would like to do further is miss the first tr as its just titles)
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Test data, please.  Please read and learn about the SSCCE, then show us the test data and the desired output.  It doesn't do anyone any good to show us some code that doesn't work, but if we can see the test data and we can understand what you want to get from it (and why - your rules are important) then we can almost always give you a good answer!

I've got to be away this morning, but I'll check back later in the day.  Best regards, ~Ray
Avatar of Julian Hansen
Change to

 foreach($obj->find('tr') as $article) 

Open in new window


$table is a string as a result of the saveHTML call.

If you want to traverse the table object then use the $obj to do so
Avatar of Member_2_5230414
Member_2_5230414

ASKER

@julianH i added the above but get a new error " Call to undefined method DOMElement::find()"


test data is (this is what  $table = $dom->saveHTML($obj); produces)

<table width="100%">
<tr class="databreakdown2253">
<th><a href="races.php?id=7456074&amp;sortby=1">Place</a></th>
<th>Dist Bt</th>
<th>Stall</th>
<th>Horse</th>
<th>Age</th>
<th><a href="races.php?id=7456074&amp;sortby=3">Weight</a></th>
<th>Headgear</th>
<th>OR</th>
<th>Trainer</th>
<th><a href="races.php?id=7456074&amp;sortby=2">Odds</a></th>
<th>Jockey (Claim)</th>
</tr>
<tr>
<td class="databreakdown2253">1st</td>
<td class="databreakdown2253"></td>
<td class="databreakdown2253">0</td>
<td class="databreakdown2253"><a href="horses.php?id=296991">Minella Present (IRE)</a></td>
<td class="databreakdown2253">5</td>
<td class="databreakdown2253">10-6</td>
<td class="databreakdown2253"></td>
<td class="databreakdown2253">0</td>
<td class="databreakdown2253"><a href="trainers.php?id=4328">Mulholland, N P</a></td>
<td class="databreakdown2253">11/10 (Fav) </td>
<td class="databreakdown2253">
<a href="jockeys.php?id=6587">McIntyre, Mr M J</a> (8)</td>
</tr>
<tr class="databreakdown18"><td colspan="12">held up, closed 3rd, led 3 out, clear when mistake last, comfortably</td></tr>
<tr>
<td class="databreakdown2253">2nd</td>
<td class="databreakdown2253">4.5</td>
<td class="databreakdown2253">0</td>
<td class="databreakdown2253"><a href="horses.php?id=308667">Derrintogher Bliss (IRE)</a></td>
<td class="databreakdown2253">5</td>
<td class="databreakdown2253">11-0</td>
<td class="databreakdown2253"></td>
<td class="databreakdown2253">0</td>
<td class="databreakdown2253"><a href="trainers.php?id=1202">Bailey, K C</a></td>
<td class="databreakdown2253">17/2  </td>
<td class="databreakdown2253">
<a href="jockeys.php?id=7216">Shoemark, Mr C P</a> </td>
</tr>
<tr class="databreakdown18"><td colspan="12">tracked leaders, closed 3 out, challenged approaching next, no impression on winner last, stayed on same pace</td></tr>
<tr>
<td class="databreakdown2253">3rd</td>
<td class="databreakdown2253">26</td>
<td class="databreakdown2253">0</td>
<td class="databreakdown2253"><a href="horses.php?id=295420">Dr Dalwhinny</a></td>
<td class="databreakdown2253">5</td>
<td class="databreakdown2253">10-8</td>
<td class="databreakdown2253"></td>
<td class="databreakdown2253">108</td>
<td class="databreakdown2253"><a href="trainers.php?id=410">McCain Jnr, D</a></td>
<td class="databreakdown2253">6/1  </td>
<td class="databreakdown2253">
<a href="jockeys.php?id=6840">James Cowley</a> (6)</td>
</tr>
<tr class="databreakdown18"><td colspan="12">led until 5th, led after 7th, headed 3 out, weakened before next</td></tr>
<tr>
<td class="databreakdown2253">4th</td>
<td class="databreakdown2253">5</td>
<td class="databreakdown2253">0</td>
<td class="databreakdown2253"><a href="horses.php?id=284908">Filatore (IRE)</a></td>
<td class="databreakdown2253">5</td>
<td class="databreakdown2253">10-11</td>
<td class="databreakdown2253"><a title="Cheekpieces worn">CkPc</a></td>
<td class="databreakdown2253">116</td>
<td class="databreakdown2253"><a href="trainers.php?id=181">Llewellyn, B J</a></td>
<td class="databreakdown2253">2/1  </td>
<td class="databreakdown2253">
<a href="jockeys.php?id=6708">Williams, Mr R J</a> (3)</td>
</tr>
<tr class="databreakdown18"><td colspan="12">last and behind after jumping left and mistake 1st, well beaten 3 out</td></tr>
<tr>
<td class="databreakdown2253">5th</td>
<td class="databreakdown2253">0.5</td>
<td class="databreakdown2253">0</td>
<td class="databreakdown2253"><a href="horses.php?id=307934">Killimordaly (IRE)</a></td>
<td class="databreakdown2253">5</td>
<td class="databreakdown2253">10-9</td>
<td class="databreakdown2253"> <a title="Tongue Tie worn">TT</a>
</td>
<td class="databreakdown2253">0</td>
<td class="databreakdown2253"><a href="trainers.php?id=147">Turnell, Andrew</a></td>
<td class="databreakdown2253">40/1  </td>
<td class="databreakdown2253">
<a href="jockeys.php?id=6900">Hamill, Mr M</a> (5)</td>
</tr>
<tr class="databreakdown18"><td colspan="12">pressed leader, led 5th, jumped left and not fluent 6th, jumped left next, soon headed, weakened after 3 out</td></tr>
</table>

Open in new window

You need to access the childNodes on the $obj. See below
<pre>
<?php
  $html = file_get_contents('e006.html');
  $dom = new DOMDocument();
  // load html
  @$dom->loadHTML($html);
  $xpath = new DOMXPath($dom);

  //this will gives you all td with class name is jobs.
  $my_xpath_query = '//table/tr/td[contains(@class, "databreakdown")]';
  $result_rows = $xpath->query($my_xpath_query);
  
  if ($result_rows->length > 0) {
    $obj = $result_rows->item(0)->parentNode->parentNode;
    // traverse the child nodes
    foreach($obj->childNodes as $article) {
      var_dump($article);
    }
    // get the text version of the table and dumpit
    $table = $dom->saveHTML($obj);
    echo $table;
  }
  else {
    echo "Table not found";
  }
?>
</pre>

Open in new window

ahh ok so the ouput is

object(DOMElement)#117 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(68) "PlaceDist BtStall HorseAgeWeightHeadgearORTrainer OddsJockey (Claim)" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(68) "PlaceDist BtStall HorseAgeWeightHeadgearORTrainer OddsJockey (Claim)" } object(DOMElement)#119 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(70) "1st0 Area Fifty One 611-0 0 Henderson, N J 5/4 (Fav) Tinkler, Andrew " ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(70) "1st0 Area Fifty One 611-0 0 Henderson, N J 5/4 (Fav) Tinkler, Andrew " } object(DOMElement)#117 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(116) "tracked leader, not fluent 4th or 5th, challenged 2 out, soon ridden, stayed on under pressure flat, led last stride" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(116) "tracked leader, not fluent 4th or 5th, challenged 2 out, soon ridden, stayed on under pressure flat, led last stride" } object(DOMElement)#119 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(68) "2ndHD0 Andiamu (FR) 411-0 TT 117 Greatrex, W J 4/1 Sheehan, Gavin " ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(68) "2ndHD0 Andiamu (FR) 411-0 TT 117 Greatrex, W J 4/1 Sheehan, Gavin " } object(DOMElement)#117 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(66) "led, mistake 3 out, pressed 2 out, driven flat, headed last stride" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(66) "led, mistake 3 out, pressed 2 out, driven flat, headed last stride" } object(DOMElement)#119 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(77) "3rd190 Arty Campbell (IRE) 410-9 0 Llewellyn, B J 28/1 Williams, Mr R J (5)" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(77) "3rd190 Arty Campbell (IRE) 410-9 0 Llewellyn, B J 28/1 Williams, Mr R J (5)" } object(DOMElement)#117 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(74) "chased leaders, not fluent 1st or 6th, ridden before 3 out, well held next" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(74) "chased leaders, not fluent 1st or 6th, ridden before 3 out, well held next" } object(DOMElement)#119 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(74) "4th190 Vazaro Delafayette (FR) 511-0Hood 0 Pipe, D E 4/1 Scudamore, Tom " ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(74) "4th190 Vazaro Delafayette (FR) 511-0Hood 0 Pipe, D E 4/1 Scudamore, Tom " } object(DOMElement)#117 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(82) "mid-division, modest headway to go 4th before 3 out, never any chance with leaders" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(82) "mid-division, modest headway to go 4th before 3 out, never any chance with leaders" } object(DOMElement)#119 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(69) "5th40 Shear Rock (IRE) 411-0Hood 0 Longsdon, C E 16/1 Fehily, Noel " ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(69) "5th40 Shear Rock (IRE) 411-0Hood 0 Longsdon, C E 16/1 Fehily, Noel " } object(DOMElement)#117 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(93) "behind, not fluent 1st, mistake next, not fluent 6th, some late progress but never any chance" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(93) "behind, not fluent 1st, mistake next, not fluent 6th, some late progress but never any chance" } object(DOMElement)#119 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(66) "6th220 Barton Antix 511-0 0 Mulholland, N P 50/1 Byrne, Michael " ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(66) "6th220 Barton Antix 511-0 0 Mulholland, N P 50/1 Byrne, Michael " } object(DOMElement)#117 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(32) "held up in rear, well behind 6th" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(32) "held up in rear, well behind 6th" } object(DOMElement)#119 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(70) "7th1.50 Get Back To Me (IRE) 711-0 0 Earle, Simon 25/1 Brennan, P J " ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(70) "7th1.50 Get Back To Me (IRE) 711-0 0 Earle, Simon 25/1 Brennan, P J " } object(DOMElement)#117 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(42) "towards rear, mistake 4th, well behind 6th" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(42) "towards rear, mistake 4th, well behind 6th" } object(DOMElement)#119 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(63) "8th4.50 Bobonyx 410-9 0 Turnell, Andrew 150/1 Banks, Mr J (5)" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(63) "8th4.50 Bobonyx 410-9 0 Turnell, Andrew 150/1 Banks, Mr J (5)" } object(DOMElement)#117 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(29) "towards rear, well behind 6th" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(29) "towards rear, well behind 6th" } object(DOMElement)#119 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(65) "9th10 Aldwick Bay (IRE) 611-0 0 Bowen, P 12/1 Johnson, Richard " ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(65) "9th10 Aldwick Bay (IRE) 611-0 0 Bowen, P 12/1 Johnson, Richard " } object(DOMElement)#117 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(96) "not fluent, tracked leaders, not fluent and sometimes jumped right, weakening when mistake 3 out" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(96) "not fluent, tracked leaders, not fluent and sometimes jumped right, weakening when mistake 3 out" } object(DOMElement)#119 (18) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(67) "PU0 Indian Jack (IRE) 611-0 0 Mullins, J W 10/1 Thornton, Andrew " ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(67) "PU0 Indian Jack (IRE) 611-0 0 Mullins, J W 10/1 Thornton, Andrew " } object(DOMElement)#117 (17) { ["tagName"]=> string(2) "tr" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "tr" ["nodeValue"]=> string(90) "mid-division, mistake 1st, not fluent 3rd or 5th, soon well behind, pulled up before 2 out" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "tr" ["baseURI"]=> NULL ["textContent"]=> string(90) "mid-division, mistake 1st, not fluent 3rd or 5th, soon well behind, pulled up before 2 out" }

Open in new window


so i take it foreach($obj->childNodes as $article) {
             
               
$horsename=  $article->find('td', 2)->plaintext;
                        
}

wouldnt be the way to do this as i got the error call to undefined method DOMElement::find()
In this case yes it will work as the <tr>'s are children of the table - find is not a method on the DOMDocument.
it says here find is :S -> http://simplehtmldom.sourceforge.net/manual.htm

what would i need to put instead???
In the example here it 100% says find

// Find all article blocks
foreach($html->find('div.article') as $article) {
    $item['title']     = $article->find('div.title', 0)->plaintext;
    $item['intro']    = $article->find('div.intro', 0)->plaintext;
    $item['details'] = $article->find('div.details', 0)->plaintext;
    $articles[] = $item;
}
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The code you are using for traversing the html is based on the DOMDocument library - you are then trying to issue a find with this object - which won't work.
so what do i need to use?
One or the other - pick the one that best suites your requirements. We don't have enough information on what you are trying to achieve to say which to go for.

If it was me I would go with the DOMDocument as it does not require an external load - however if there are functions in the other library that you need then that answers the question.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As a side note, it would help tremendously to actually see a sample HTML page as well as what you expect to see in the results. It's a little "quirky" to find the <td>, then go back UP to the <table>, then loop through each row and then get the results. There may be a cleaner way to do it, but it's hard to know without seeing a sample HTML page.

Also, your code indicated the same <td> for "horse" and "place" - I kept that same index in my sample code above, but my guess is that "horse" should be index 1, not index 2.
In the test data posted here, there is only one table.  It follows that returning that table satisfies the test case.  Almost certainly this is not what you want.  
https://www.experts-exchange.com/questions/28535162/php-sime-html-dom-error-Call-to-a-member-function-find-on-this-line-foreach-table-find-'tr'-as-article.html?anchorAnswerId=40372784#a40372784

In an earlier question we had a more complete HTML document, but there was no table in the document that satisfied the test case.
https://www.experts-exchange.com/questions/28533808/PHP-Scraper-if-table-contains-td-with-class-grab-all-table.html?anchorAnswerId=40370294#a40370294

The closer we can get to your actual data set, the more likely we will be to get you an acceptable answer.  There are many ways to "skin this cat," but without visibility of the test data and the expected output all we can do is make some guesses and talk about the solutions.