Jump to content

get table?


Go to solution Solved by Ch0cu3r,

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.

Edited by moiisam10
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;
}

Edited by Ch0cu3r
Link to comment
https://forums.phpfreaks.com/topic/297815-get-table/#findComment-1519004
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

  • Solution

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 by Ch0cu3r
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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