Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php Dom Document -> searching withing a child node

Posted on 2014-10-15
5
Medium Priority
?
160 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
ID: 40382586
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
ID: 40382832
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 2000 total points
ID: 40382916
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
ID: 40383070
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
ID: 40385319
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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

916 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