Solved

php Dom Document -> searching withing a child node

Posted on 2014-10-15
5
115 Views
Last Modified: 2014-11-10
Hi all,

Im currently able to get the values from a table and get the 8th item
$trainer =  $article->childNodes->item(8)->nodeValue;

Open in new window


the out put is like so G LeeR Varian

now within this item(8) of the childnode is the following

<td class="winner">
				<a href="/jockey/497-g-lee">G Lee</a><br />
				<em><a href="/trainer/3372-r-varian">R Varian </a></em>
			</td>

Open in new window


How can i do it so
If A Herf contains the value  jockey then $jockey = G Lee
If A Herf contains the value trainer then $trainer =R Varian
0
Comment
Question by:runnerjp2005
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
Instead of returning the nodeValue of item[8] you should be able to look at the childNodes of item[8] which I assume is the row, and the child nodes for the tag <td> cell should also contain childNodes for the tags it con tains.  You just have to walk it down the tree until to get the level of detail you want.

Cd&
0
 

Author Comment

by:runnerjp2005
Comment Utility
im not 100% sure what you mean.


below is my code after getting the tble i wanted:

$my_xpath_query = '//table[contains(@class, "result")]';
$result_rows = $xpath->query($my_xpath_query);
if ($result_rows->length > 0) {
    // IF WE DID GET THE PARENT'S <tr> PARENT'S <table> NODE
    $obj = $result_rows->item(0);
    foreach ($obj->childNodes as $article) {
        // set count as plsu one so i know how many times it has looped
        $count = $count + 1;
        // if this is the first loop ignor as its the table titles
        if ($count != 1) {
            // starst by getting the place and the comments
            //ceck to see if the is the evens count meaning its the horses place
            if ($count % 2 == 0) {
                $comments = "";
                $place = $article->childNodes->item(0)->nodeValue . "<br />";
            }
            //else its the comments
            else {
                $place = "";
                $comments = $article->childNodes->item(0)->nodeValue . "<br />";
            }

            $distance_bt = $article->childNodes->item(14)->nodeValue;
            $stall = $article->childNodes->item(2)->nodeValue;
            $horse = $article->childNodes->item(6)->nodeValue;
            //$age = $article->childNodes->item(6)->nodeValue;
            $weight = $article->childNodes->item(7)->nodeValue;
            //$headgear =  $article->childNodes->item(8)->nodeValue;
            //$OR = $article->childNodes->item(10)->nodeValue;??
            $trainer = $article->childNodes->item(8)->nodeValue;
            $jockey = $article->childNodes->item(16)->nodeValue;
            $odds = $article->childNodes->item(16)->nodeValue;


        }

        //need to put database add here!!!
        // echo   $comments;
        //echo $place;
        echo $trainer . "<br />";
        // $distance_bt .$stall .$weight.$headgea.$trainer.$odds .$jockey.$horse
    }

}

Open in new window



here is the  page source from which i get the table from view-source:http://www.racinguk.com/results/race/113234
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
Comment Utility
I guess I don't understand what you are asking.

With the structure:
<td class="winner">
				<a href="/jockey/497-g-lee">G Lee</a><br />
				<em><a href="/trainer/3372-r-varian">R Varian </a></em>
			</td>

Open in new window

the node for the td has a property childNodes which is a collection of the nodes containing the value of the <a> tag for the jockey and the <em> tag which has a childNode for the <a>tag containing the trainer.

Instead of trying to scrape the page why don't you just ask the owners of that site for a feed or see if they have an API you can use.  Using scraping and script against the Nodes collection means that if they make a change in the page layout it will likely break your script.

I don't see a copyright notice on that site, but they appear to be selling subscription services, so if they become aware of the scraping, they may take steps to block you.

Cd&
0
 

Author Comment

by:runnerjp2005
Comment Utility
saldy they dont have an api so will need to pull it from the site itself.  i can grab all the rows its just seperating the jockey and the trainer apart that im struggling with
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
Separating them is just a matter of getting down to enough detail. each tag is a node within its parent element's  node collect the jockey and the trainer are each in their own <a> tag so all you need is to come down from the node for the <td>

So you do what you are doing now, and when you get to the <td> you you just look through its childNodes to find the nodes where the tagName is A and then grab the text.

Cd&
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now