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)
runnerjp2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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
0
Julian HansenCommented:
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
0
runnerjp2005Author Commented:
@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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Julian HansenCommented:
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

0
runnerjp2005Author Commented:
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()
0
Julian HansenCommented:
In this case yes it will work as the <tr>'s are children of the table - find is not a method on the DOMDocument.
0
runnerjp2005Author Commented:
it says here find is :S -> http://simplehtmldom.sourceforge.net/manual.htm

what would i need to put instead???
0
runnerjp2005Author Commented:
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;
}
0
Julian HansenCommented:
Are you mixing your libraries - your code snippet has this

$dom = new DOMDocument();

Open in new window


Which refers to http://php.net/manual/en/domdocument.construct.php

But you are also including the PHP imple_html_dom.php library which is not the same thing?
0
Julian HansenCommented:
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.
0
runnerjp2005Author Commented:
so what do i need to use?
0
Julian HansenCommented:
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.
0
gr8gonzoConsultantCommented:
Julian is correct. You're mixing two different libraries that do similar things. I suggested SimpleHTMLDom in the other thread just as my preferred option - mostly because it is very light on dependencies and several clients of mine don't have DOMDocument available in their PHP build, but I think it's easier to use, too.

You can use either one, but not both at the same time. That would be like trying to drive two cars at once. You're sitting in one car and reaching over to the other one to try and steer that one.  

Julian can probably steer you in how to use DOMDocument. If you want to JUST use SimpleHTMLDom instead:
<?
// Load the SimpleHTMLDom library
include 'simple_html_dom.php';

// Get the $content
$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);
	
// Load the $content into a SimpleHTMLDom document.
$html = str_get_html($content);	

// Initialize a container for the data we'll be parsing
$data = array();

// Find all <td>s where class is 'databreakdown'
$tds = $html->find('td.databreakdown');
if(count($tds) > 0)
{
	// Loop through each resulting <td>
	foreach($tds as $td)
	{
		// Find the parent <table>
		$table = $td->parent()->parent();
		
		// Save its HTML
		$tableHTML = $table->outertext();
		
		// Loop through all rows (<tr>) in the table
		$trs = $table->find('tr');
		foreach($trs as $tr)
		{
			// Get the <td>s for the row
			$row_tds = $tr->find('td');
			
			// Fetch the data			
			$data[] = array(
				"horse" => $row_tds[2]->plaintext,
				"place" => $row_tds[2]->plaintext,
				"odds" => $row_tds[3]->plaintext
			);
		}
	}
	
	// Display all the results
	print_r($data);
}
else
{
	echo "Table not found";
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
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.
0
Ray PaseurCommented:
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.  
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28535162.html#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.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28533808.html#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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.