php Dom Document -> searching withing a child node

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
runnerjp2005Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

COBOLdinosaurCommented:
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
runnerjp2005Author Commented:
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
COBOLdinosaurCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
runnerjp2005Author Commented:
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
COBOLdinosaurCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.