I solved my initial issue with the below code. I now need to learn how to limit the returned data to the first 5 rows. How do I limit a foreach loop?
I am scraping data from a site - I am able to traverse the DOM to get the table I want "LAST 1 MONTH (11/20/2017-12/19/2017)" which is the 3rd one or "2". However, I can't quite get the output correct. I need to wrap it in a table, with each row containing the td's specified in the code. Here is the code I am using w/limited success:
<?php
$html = file_get_contents('https://ninjatrader.isystems.com/Systems/TopStrategies');
$doc = new DOMDocument();
@$doc->loadhtml($html);
$xpath = new DOMXPath($doc);
echo "<table>";
foreach($xpath->query('//table')->item(2)->getElementsByTagName('tr') as $rows) {
$cells = $rows->getElementsByTagName('td');
echo "<tr>
<td>" . $cells->item(1)->textContent . "</td>
<td>" . $cells->item(2)->textContent . "</td>
<td>" . $cells->item(3)->textContent . "</td>
<td>" . $cells->item(5)->textContent . "</td>
</tr>";
}
echo "</table>";
?>
OK, I've pretty much solved my issue with the above. Is there a better way to do this?