0

I am using SIMPLE HTML DOM to scrape a website and I'm getting the following code...

$GameSummary

<dl class="ismSummary ismHomeSummary">
    <dt>Goals scored</dt>
    <dd><a class="ismViewProfile" href="#438">Gallas</a></dd>
    <dd><a class="ismViewProfile" href="#458">Defoe</a></dd>
    <dt>Assists</dt>
    <dd><a class="ismViewProfile" href="#443">Vertonghen</a></dd>
    <dd><a class="ismViewProfile" href="#447">Lennon</a></dd>
    <dt>Yellow cards</dt>
    <dd><a class="ismViewProfile" href="#438">Gallas</a></dd>
    <dd><a class="ismViewProfile" href="#439">Walker</a></dd>
    <dd><a class="ismViewProfile" href="#450">Huddlestone</a></dd>
    <dt>Saves</dt>
    <dd><a class="ismViewProfile" href="#433">Friedel</a> (3)</dd>
</dl>

I am trying to cut each section to its specific sections... I am trying to get the following info in the following order...

Heading1: Goals Scored
  Entry: Gallas
  Entry: Defoe
Heading2: Assists
  Entry: Vertongen
  Entry: Lennon
etc....

Here's the code I can get the headers with...

foreach ($GameSummary->find('dt') as $HeadingType)
  {
  echo $HeadingType;
  }

Gives me all headings... and to get content of each header..

foreach ($GameSummary->find('dd') as $PlayerNames)
  {
  echo $PlayerNames;
  }

What I am wondering is... How do I separate these into different groups? I can get all the headers and all the content separately, but I don't know how to get it so that each header contains its contents.

Any ideas how? It should be easy enough but my brain can't figure it out.

Thanks!

EXAMPLE AT: http://fantasypl.com/results.php

Cully
  • 484
  • 9
  • 19
  • In short, do a `->find('dl')` and get all the children from that, then do a simple loop over those children: see if it's a dt or a dd, and handle it appropriately. – Marc B Oct 31 '12 at 05:20

1 Answers1

0

Should be, roughly:

   $i=1;
   foreach ($GameSummary->find('dl')->children() as $element)
    {
       if ($element->tag == "dt")  { echo "Heading$i: $element"; $i++; }
       if ($element->tag == "dd") echo "Entry: $element";

    }

[EDIT] Actual working code, from the comment below:

    foreach ($GameSummary->children() as $test) 
     { 

        if ($test->tag == "dt") { echo "Heading$i: ".$test->innertext."<br>"; $i++;} 
        if ($test->tag == "dd") { echo "Player: ".$test->innertext."<br>";} 
      }
janenz00
  • 3,315
  • 5
  • 28
  • 37
  • Thanks.. It didn't work as-is, but I managed to get it to do what I wanted... or closer at least.. Heres what I used: `foreach ($GameSummary->children() as $test)` `{` `echo $test->tag;` `if ($test->tag == "dt") { echo "Heading$i: ".$test->innertext."
    "; $i++;}` `if ($test->tag == "dd") { echo "Player: ".$test->innertext."
    ";}` `}` Thanks - this saves me a lot of hassle. I think I can get it from here.
    – Cully Oct 31 '12 at 07:59
  • @CUlly - Thought so. I was just pointing you to the right direction. – janenz00 Oct 31 '12 at 21:09