Question

In the past, I've parsed things quite simply with something like the following:

$dom=new DOMDocument();
$dom->loadHTML(file_get_contents('http://...'));
$tables=$dom->getElementsByTagName('table');
$trs=$tables->item(0)->getElementsByTagName('tr');
$tds=$trs->item(0)->getElementsByTagName('td');
$json=array(
    "item1"=>$tds->item(0)->textContent,
    "item2"=>$tds->item(1)->textContent,
    "item2"=>$tds->item(2)->textContent,
    "item2"=>$tds->item(3)->textContent,
);

However, I need to parse a few things such that I can add their values together and obtain a sum to store in an array.

To clarify: Suppose there is a page that has a table. The number of rows in this table will vary, but in one of the columns, there are integers that I would like to add together, so that the sum of all rows of this particular column is stored in my array, with the exception of the first row, since it contains the column names.

At this stage, all I know is that I'm supposed to use a foreach statement to reliably obtain each row's values.

Was it helpful?

Solution

Since you have to skip the first element of the list, a for loop may be better. The code below totals the first column in the rows.

$total = 0;
$trs=$tables->item(0)->getElementsByTagName('tr');
for ($rownum = 1; $rownum < $trs->length; $rownum++) {
    $row = $trs->item($rownum);
    $td = $row->getElementsByTagName('td')->item(0);
    $total += $td->textContent;
}

OTHER TIPS

XPath provides a sum function that would be helpful here:

<?php

$html = '<table>
  <tr><td>heading</td><td>heading</td></tr>
  <tr><td>1</td><td>2</td></tr>
  <tr><td>4</td><td>8</td></tr>
</table>';

$doc = new DOMDocument;
$doc->loadHTML($html);

$xpath = new DOMXPath($doc);

// sum of cells in the second column, skipping the first row
print $xpath->evaluate('sum(//table//tr[position() > 1]/td[2])');

// output: 10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top