How can I get td values using dom and php - php

I have a table such this :
<table>
<tr>
<td>Values</td>
<td>5000</td>
<td>6000</td>
</tr>
</table>
And I want to get td's content. But I could not manage it.
<?PHP
$dom = new DOMDocument();
$dom->loadHTML("figures.html");
$table = $dom->getElementsByTagName('table');
$tds=$table->getElementsByTagName('td');
foreach ($tds as $t){
echo $t->nodeValue, "\n";
}
?>

There are multiple problems with this code:
To load from an HTML file, you need to use DOMDocument::loadHTMLFile(), not loadHTML() as you have done. Use $dom->loadHTMLFile("figures.html").
You can't use getElementsByTagName() on a DOMNodeList as you have done (on $table). It can only be used on a DOMDocument.
You could do something like this:
$dom = new DOMDocument();
$dom->loadHTMLFile("figures.html");
$tables = $dom->getElementsByTagName('table');
// Find the correct <table> element you want, and store it in $table
// ...
// Assume you want the first table
$table = $tables->item(0);
foreach ($table->childNodes as $td) {
if ($td->nodeName == 'td') {
echo $td->nodeValue, "\n";
}
}
Alternatively, you could just directly search for all elements with tag name td (though I'm sure you want to do that in a table-specific manner.

You should use a for loop to display the multiple td's with id attributes in it such that each td must signify a different id in html file
for example
for($i=1;$i<=10;$i++){
echo "<td id ='id_".$i."'>".$tdvalue."</td>";
}
and then again you can fetch the td values by just iterating another for loop over getElementById

The td data can be found inside childNodes
$dom = new domDocument;
$dom->loadHTML("your-url");
$tables = $dom->getElementsByTagName('table');
$rows = $tables->getElementsByTagName('tr');
foreach ($rows as $row) {
echo $row->childNodes[0]->nodeValue;
}

Related

How to get child element of DOMDocument

Trying to get the specific value from a table with tr and td elements...
HTML:
<table>
<tr>
<td>value1</td>
<td>value2</td>
<td>value3</td>
</tr>
</table>
PHP:
$html = 'http://www.example.com'; // edited
$dom = new DOMDocument;
#$dom->loadHTML($html);
$data = $dom->getElementsByTagName('tr:nth-child(3n)');
foreach ($data as $datas){
echo $link->nodeValue;
}
Using such or different approach, how to get the value of specific td element... ?
Using getElementsByTagName() returns a list of the tags based on your starting point, so once you've found the table, you can then use the same function to get the <td> tags. You can then just pick out the elements your after...
$data = "<table>
<tr>
<td>value1</td>
<td>value2</td>
<td>value3</td>
</tr>
</table>";
$dom = new DOMDocument;
$dom->loadHTML($data);
$table = $dom->getElementsByTagName('table');
$td = $table[0]->getElementsByTagName('td'); // Fetch all td elements in the first table
echo $td[2]->nodeValue; // Echo out the value of the 3rd item (zero based arrays)
Prints out..
value3
xPath can be used to get particular element. Try the following code to get 3rd td value from given html.
$html = '<table>
<tr>
<td>value1</td>
<td>value2</td>
<td>value3</td>
</tr>
</table>';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$table = $dom->getElementsByTagName('table')->item(0);
$query = 'tr/td[3]';
$entries = $xpath->query($query, $table);
echo $entries[0]->nodeValue;
Read about DOMXpath query()
Update: Use of file_get_content is also simple, you can retrieve html/xml as string in $html variable and rest of the process is same:
$html = file_get_contents("path/to/file/x.html"); // target path
I think this should help you.
Just assign class Name for td elements and then use this
for eg:
<td class="two">value2</td>
$(this).closest('tr').children('td.two').text();

get value of html table row tag using php

Table:
<table class="secondary">
<tr><td>BB:</td><td>112</td></tr>
<tr><td>CC:</td><td>99</td></tr>
<tr><td>DD:</td><td>1</td></tr>
</table>
for example I want to get third row of this table.
I know how to get values from div tag using ID, like:
$doc = new DomDocument();
$doc->loadHTMLFile('http://www.results.com');
$thediv = $doc->getElementById('result');
echo $thediv->textContent;
but how can we get the values from table row tag?
You can use DomXpath http://php.net/manual/en/class.domxpath.php
(Assuming for this example that your table is the first table on the page with class="secondary")
For example:
<?php
$doc = new DomDocument();
$doc->loadHTMLFile("filename.html");
$xpath = new DomXpath($doc);
$row = $xpath->query('//table[#class="secondary"][1]/tr[3]')->item(0);
// Get the html of the third row:
echo $doc->saveHTML($row);
// Get the values from the td's for the third row
foreach ($row->childNodes as $td) {
echo sprintf("nodeName: %s, nodeValue: %s<br>", $td->nodeName, $td->nodeValue);
}

PHP's DomXPath not working the way it was expected

I'm trying to parse this HTML page: http://www.valor.com.br/valor-data/moedas
For a simple start, I'm trying to get all td elements with class="left" and echoing their inner texts. What I'm struggling to understand is why this code:
$finder = new DomXPath($dom);
$tds = $finder->query("//*[#class='left']");
foreach ($tds as $td) {
echo $td->textContent;
}
gives me the expected output (a bunch of words that belong to those td elements which aren't worth pasting here) while this:
$finder = new DomXPath($dom);
$tds = $finder->query("//td[#class='left']");
foreach ($tds as $td) {
echo $td->textContent;
}
finds nothing. I've also tried $finder->query("//td") to simply get all td elements, but it's like DomXPath doesn't recognize tag names. Has anyone ever faced this same problem?
I have not tested, but this is probably a namespace issue. Your input page is XHTML and has correctly declared an XHTML namespace. Therefore, you need to register a namespace prefix and use that prefix in your query.
Something like this
$finder = new DomXPath($dom);
$finder->registerNamespace("x", "http://www.w3.org/1999/xhtml");
$tds = $finder->query("//x:td[#class='left']");
foreach ($tds as $td) {
echo $td->textContent;
}

confused with xpath

I've got this PHP code loading in some html.
$dom = new DOMDocument();
$dom->loadHTML($somehtml);
$xpath = new DOMXPath($dom);
$divContent = $xpath->query('//table[class="defURLP"]');
echo $divContent;
I'm too confused to understand quite what needs to go on here, however my desire would it to be able to populate the variable $divContent to have the html contents of the table with the classname defURLP
It's currently just returning
object(DOMNodeList)#3 (0) { }
You need to retrieve the first item from the DOMNodeList returned by your xpath query, since there may be more than one in the list.
// Queries for tables having class defURLP
$tables = $xpath->query('//table[class="defURLP"]');
// Reference the first one in $divContent
$divContent = $tables->item(0);
// Output its nodeValue
echo $divContent->nodeValue;
Or iterate over the node list with a foreach:
$tables = $xpath->query('//table[class="defURLP"]');
// Iterate over the whole node list in $tables (if it is multiple nodes)
foreach ($tables as $t) {
echo $t->nodeValue;
}

PHP XPath Table elements disapearing

I have just learned about XPath and I am wanting to read data from only certain columns in a table.
My current code looks like this:
<?php
$file_contents = file_get_contents('test.html');
$dom_document = new DOMDocument();
$dom_document->loadHTML($file_contents);
//use DOMXpath to navigate the html with the DOM
$dom_xpath = new DOMXpath($dom_document);
$elements = $dom_xpath->query("//tr[#class='rowstyle']");
if (!is_null($elements)) {
foreach ($elements as $element)
{
echo $element->nodeValue . '<br />';
}
}
else
{
echo 'none';
}
?>
Also a variation in the query because through my research I have seen lots of issues with nest table elements but it produces the same result:
$elements = $dom_xpath->query("//table[#class='tablestyle']/tbody/tr[#class='rowstyle']");
It does grab the row of data but it makes into a single string, combining all of the cells into one string and making the tags disappear.
What I really want to do is separate those cells and grab the certain row number.
I am also curious on how to find out which version of XPath I have... My PHP version is 5.3.5
Its not combining those cells... youre outputting the nodeValue which in this case is behaving like innerHTML. IF you want to work on the cells themselves the either use childNodes or a xpah query using the row as the context, then loop over the cells.
Example:
$dom_xpath = new DOMXpath($dom_document);
$elements = $dom_xpath->query("//tr[#class='rowstyle']");
foreach ($elements as $element)
{
foreach($element->childNodes as $cell) {
echo $cell->nodeValue . '<br />';
}
}

Categories