Solved

php Dom Document -> searching withing a child node

Posted on 2014-10-15
5
142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add Rows on a Table 8 54
Securing source code... theoretical question 10 48
Secure registration PHP 7 43
PHP, Curl and XML 13 15
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. …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

739 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