I am parsing some table data using Xpath and need to echo the header row plus any rows that contain certain data. I can successfully select what I want but am not sure of the syntax to output the entire rows including the html tr and th tags.
Here is my code. I am not sure how to simplify the verbose output of the selected lines. I tried the commented out line but it strips the tags.
It is similar to this question: XPath in PHP Removes HTML Tags but I do not understand the answer.
$xpath = new DOMXPath($doc);
$rows = $xpath->query('//table//tr'); // extract rows from html
echo '<table>';
$first = true;
foreach ($rows as $row) { // process each row of results
if ($first) { // echo header row
echo '<tr><td>'.$td->item(0)->textContent.'</td><td>'.$td->item(1)->textContent.'</td><td>'.$td->item(2)->textContent.
'</td><td>'.$td->item(3)->textContent.'</td><td>'.$td->item(4)->textContent.'</td><td>'.$td->item(5)->textContent.
'</td><td>'.$td->item(6)->textContent.'</td><td>'.$td->item(7)->textContent.'</td></tr>';
$first = false;
}
else {
$td = $xpath->query('td', $row);
$firstnm = trim(strtok($td->item(1)->textContent," "));
$mmyyyy = substr($td->item(3)->textContent,2,7);
if ($firstnm == $firstname && $mmyyyy == $thismth)
echo '<tr><td>'.$td->item(0)->textContent.'</td><td>'.$td->item(1)->textContent.'</td><td>'.$td->item(2)->textContent.
'</td><td>'.$td->item(3)->textContent.'</td><td>'.$td->item(4)->textContent.'</td><td>'.$td->item(5)->textContent.
'</td><td>'.$td->item(6)->textContent.'</td><td>'.$td->item(7)->textContent.'</td></tr>';
// THIS STRIPS THE TAGS echo $row->nodeValue;
}
}
}
echo '</table>';