Solved

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

Posted on 2014-10-10
15
410 Views
Last Modified: 2014-10-15
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)
0
Comment
Question by:runnerjp2005
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40372716
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40372726
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
 

Author Comment

by:runnerjp2005
ID: 40372784
@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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40372809
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
 

Author Comment

by:runnerjp2005
ID: 40372857
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40372869
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
 

Author Comment

by:runnerjp2005
ID: 40372882
it says here find is :S -> http://simplehtmldom.sourceforge.net/manual.htm

what would i need to put instead???
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:runnerjp2005
ID: 40372932
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
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 40372937
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40372940
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
 

Author Comment

by:runnerjp2005
ID: 40372954
so what do i need to use?
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40372974
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
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 40373227
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
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 40373237
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40373506
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now