Jump to content

get table?


moiisam10

Recommended Posts

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"..

 

screenshot_25.png

 

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.

Link to comment
https://forums.phpfreaks.com/topic/297815-get-table/
Share on other sites

 

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..

 

 

screenshot_26.png

 

But i just need to get this. 

 

screenshot_27.png

 

and that info is on this <tr> tag.

screenshot_29.png

 

Thanks in advanced.

Link to comment
https://forums.phpfreaks.com/topic/297815-get-table/#findComment-1519005
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/297815-get-table/#findComment-1519010
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/297815-get-table/#findComment-1519049
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.