moiisam10 Posted August 16, 2015 Share Posted August 16, 2015 (edited) Hello guys, am here again... I have a problem triying to read the total ammount of a table.. I load the whole table with xpath. $html = file_get_html('$url'); $rows = $html->find('//*[@class="listado1"]'); foreach($rows as $row) { echo $row->plaintext; } but i need to get just the "total".. i tryed with. $html = file_get_html('$url'); $rows = $html->find('//*[@class="listado1"][5]'); foreach($rows as $row) { echo $row->plaintext; } //NOTHING.... $html = file_get_html('$url'); $rows = $html->find('//*[@class="listado1"]//tr//td/b'); foreach($rows as $row) { echo $row->plaintext; } //NOTHING AGAIN.... Thanks in advanced to all. Edited August 16, 2015 by moiisam10 Quote Link to comment https://forums.phpfreaks.com/topic/297815-get-table/ Share on other sites More sharing options...
Ch0cu3r Posted August 16, 2015 Share Posted August 16, 2015 (edited) Remove single quotes around $url on this line $html = file_get_html('$url'); Variables in single quotes are not parsed. $html = file_get_html($url); $rows = $html->find('//td[@class="listado1"]'); foreach($rows as $row) { echo $row->plaintext; } Edited August 16, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/297815-get-table/#findComment-1519004 Share on other sites More sharing options...
moiisam10 Posted August 16, 2015 Author Share Posted August 16, 2015 Remove single quotes around $url on this line $html = file_get_html('$url'); Variables in single quotes are not parsed. $html = file_get_html($url); $rows = $html->find('//td[@class="listado1"]'); foreach($rows as $row) { echo $row->plaintext; } i make it work but i get all the text in the table. like this.. But i just need to get this. and that info is on this <tr> tag. Thanks in advanced. Quote Link to comment https://forums.phpfreaks.com/topic/297815-get-table/#findComment-1519005 Share on other sites More sharing options...
Solution Ch0cu3r Posted August 16, 2015 Solution Share Posted August 16, 2015 (edited) It seems all td cells have the same class. You are going to need to use a much more complex xpath query using DOMDocument $doc = new DomDocument(); $doc->loadHTMLFile($url); $xpath = new DOMXpath($doc); // this xpath query first finds the <td> cell with class="listado1" and contains <b>TOTAL</b> will then return the contents of the next <td> cell $row = $xpath->query("//td/b[preceding::td[@class='listado1']/b[.='TOTAL']]"); echo $row[0]->nodeValue; Above tested with the following HTML <table> <tr> <td colspan="3" align="right" class="listado1"> <b>whatever here</b> </td> <td align="right" class="listado1"> <b>whatever here</b> </td> </tr> <tr> <tr> <td colspan="3" align="right" class="listado1"> <b>TOTAL</b> </td> <td align="right" class="listado1"> <b>4,890.20</b> " " </td> </tr> <tr> <td colspan="3" align="right" class="listado1"> <b>whatever here</b> </td> <td align="right" class="listado1"> <b>whatever here</b> </td> </tr> </table> Returns 4,890.20 Edited August 16, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/297815-get-table/#findComment-1519010 Share on other sites More sharing options...
moiisam10 Posted August 17, 2015 Author Share Posted August 17, 2015 It seems all td cells have the same class. You are going to need to use a much more complex xpath query using DOMDocument $doc = new DomDocument(); $doc->loadHTMLFile($url); $xpath = new DOMXpath($doc); // this xpath query first finds the <td> cell with class="listado1" and contains <b>TOTAL</b> will then return the contents of the next <td> cell $row = $xpath->query("//td/b[preceding::td[@class='listado1']/b[.='TOTAL']]"); echo $row[0]->nodeValue; Above tested with the following HTML <table> <tr> <td colspan="3" align="right" class="listado1"> <b>whatever here</b> </td> <td align="right" class="listado1"> <b>whatever here</b> </td> </tr> <tr> <tr> <td colspan="3" align="right" class="listado1"> <b>TOTAL</b> </td> <td align="right" class="listado1"> <b>4,890.20</b> " " </td> </tr> <tr> <td colspan="3" align="right" class="listado1"> <b>whatever here</b> </td> <td align="right" class="listado1"> <b>whatever here</b> </td> </tr> </table> Returns 4,890.20 God. Thanks in advanced. Quote Link to comment https://forums.phpfreaks.com/topic/297815-get-table/#findComment-1519049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.