Jump to content

Looping with XPath in php to get tables values


peppegiuseppe

Recommended Posts

I have this html table:

<tbody><tr>..</tr><tr><td class="tbl_black_n_1">1</td><td class="tbl_black_n_1" nowrap="" align="center">23/07/14 08:10</td><td class="tbl_black_n_1"><img src="http://www.betonews.com/img/SportId389.gif" width="10" height="10" border="0" alt=""></td><td class="tbl_black_n_1"></td><td class="tbl_black_n_1" nowrap="" align="center">BAK WS</td><td class="tbl_black_n_1" nowrap="" align="right">M. Eguchi</td><td class="tbl_black_n_1" align="center">-</td><td class="tbl_black_n_1" nowrap="">Radwanska U. </td><td class="tbl_black_n_1" align="center" title=" ">1,02</td><td class="tbl_black_n_1" align="center"><td class="tbl_black_n_1" align="center" title=" "> </td><td class="tbl_black_n_1" align="center"><td class="tbl_black_n_1" align="center" title=" ">55,00</td><td class="tbl_black_n_1" align="center"><td class="tbl_black_n_1" align="right">86%</td><td class="tbl_black_n_1" align="right">-</td><td class="tbl_black_n_1" align="right">14%</td><td class="tbl_black_n_1" align="center" title=" ">524.647</td><td class="tbl_black_n_1" nowrap=""><a href="popup.asp?tp=2100&lang=en&idm=553759" target="_blank"><img src="http://www.betonews.com//img/i_betfair.gif" width="12" height="10" border="0" alt=""></a><a href="popup.asp?tp=2110&lang=en&idm=553759" target="_blank"><img src="http://www.betonews.com//img/i_history.gif" width="12" height="10" border="0" alt=""></a></td></tr><tr>..</tr><tr>..</tr><tr>..</tr>...</tbody>

There are more than one hundred <tr> structured at the same way, which contain lots of <td>.I manage to loop with xpath to store all data in a database except one: the last <td> element.. i want the href attribute of the first <a>. So, in my example: "popup.asp?tp=2100&lang=en&idm=553759", but when i run my query, id variable retrieve a NULL value. Help!!

This is my php code, but I can not go on..

$document = new DOMDocument();$document->loadHTML($response);$xpath = new DOMXPath($document);$expression = '(//table[@cellpadding="3"])[1]/tr[position() > 1]';$rows = $xpath->query($expression);foreach ($rows as $row) {  $result = array();  $td = $row->childNodes;    $data = $td->item(2)->nodeValue;  $immagine = $td->item(4)->firstChild->attributes->getNamedItem("src")->nodeValue;  $team1 = $td->item(10)->nodeValue;  $team1 = stripText($team1);  $team2 = $td->item(14)->nodeValue;  $team2 = stripText($team2);  $id = $td->item(36)->childNodes->item(1)->attributes->getNamedItem("href")->nodeValue‌​;

 

Edited by peppegiuseppe
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...