0

I am using SimpleHTMLDOM and I have two tables with identical columns and I need to extract the column titles?

Sample Picture

Below is what I am using to get all the data but now I need to only select the Month and corresponding column titles (jan, feb, etc)

 $r_tables = $dom->find('table');

foreach($r_tables as $table) {
    $r_cells = $table->find('td');

    foreach($r_cells as $cell) {
        echo $cell->plaintext.'<br />';
    }
}
KPO
  • 890
  • 2
  • 20
  • 40

4 Answers4

1

I think what you are looking for is ...

$tables = $dom->find('table');

foreach($tables as $table) {
    $r_cells = $table->find('tr');
    $i = 0;
    foreach($r_cells as $row) {
        $cell = $row->find('td');
        if ($i == 0) {
            foreach($cell as $td) { 
                echo $td.'<br />';
            }
        }  
    $i++;  
    }
}
Herb
  • 345
  • 1
  • 17
  • Thankss again @herb. This worked but it repeats the results twice... Could it be because I have 2 tables with identical column names? – KPO Feb 11 '13 at 23:41
  • ALso, if I wanted to organize all this in an html table, how would i go about doing that? – KPO Feb 11 '13 at 23:43
  • I am sort of new to this but can you clarify how you were able to do this? It would help me figure out the rest. – KPO Feb 12 '13 at 00:14
  • @KPO - yes the line foreach($tables as $table) loops through every table on the page – Herb Feb 12 '13 at 14:18
0

a crude way would be

$i = 0;
foreach($r_tables as $table) {
$r_cells = $table->find('td');

foreach($r_cells as $cell) {
  if($i == 0) {    
    echo $cell->plaintext.'<br />';
  }    
}
$i++;

}

Herb
  • 345
  • 1
  • 17
  • 1
    Couldn't you just do `echo $r_cells[0]->plaintext;`? (never used SimpleHTMLDom) – Supericy Feb 11 '13 at 23:08
  • Thanks @herb. This works but it also shows the second row, which i don't want. How should I remove that and only have month row? – KPO Feb 11 '13 at 23:08
  • I would need to see your code behind $table->find('td') but i presumed $r_cells was a table row – Herb Feb 11 '13 at 23:11
  • @herb Can you please clarify what exactly you need to see? – KPO Feb 11 '13 at 23:14
  • I would need to see what was behind the $table->find('td') object method. Does it return a row at a time. I am thinking because you got back everything that you might be trying to execute something along the lines of $table->find('tr') before you divide it up into individual cells – Herb Feb 11 '13 at 23:21
  • @herb I don't have anything else. All the object method code is in the question. I am not sure what else to add there.. – KPO Feb 11 '13 at 23:24
  • @supericy I used your suggestion and it seems to do the trick but it returns the results 5 times. Why is it doing that? – KPO Feb 11 '13 at 23:32
  • @KPO Remove the second for loop (ie `foreach ($r_cells as cell)`). And just have the 1 call. I added my own answer to your question as well, you can take a look there for the exact code. – Supericy Feb 12 '13 at 00:37
0

Find the td's from the first tr of the first table:

$tds = $dom->find('table', 0)->find('tr', 0)->find('td');

Map the text of each td into an array:

$headers = array_map(function($td){return $td->text();}, $tds);
pguardiario
  • 53,827
  • 19
  • 119
  • 159
0

This should do what you want (If I understand correctly):

foreach($r_tables as $table) {
    $r_cells = $table->find('td');

    echo $r_cells[0]->plaintext . '<br />';;
}
Supericy
  • 5,866
  • 1
  • 21
  • 25