We help IT Professionals succeed at work.
Get Started

simple html dom php accessing table/cell  values

weekapaug
weekapaug asked
on
2,744 Views
Last Modified: 2017-03-11
Cant get at the table values with simple_html_dom.

Basically I need to first access the table with ID of mainTable, then loop simultaneously through both the TH value and Span Class

I tried the same way I do this with DIVS and it does not pick up any data....

I tried below and also tried using innertext vs plaintext and neither of those do anything.  I also tried just coding it to loop through anything with the 'tr' tag, avoiding asking for a table, and it still does nothing.

foreach ($html->find('table') as $div_element){
      
          foreach ($div_element->find('th') as $pcts){
                 $pct[] = $pcts->plaintext;
           }

          foreach ($div_element->find('span[class=valueIneed]') as $pcus){
                 $pcu[] = $pcus->plaintext;
           }
}

Below is the data example and my code returns empty, but if I use this for divs it works great. It seems like maybe there is something special to do for table data?

<table id="mainTable">
    <tr id="LargeCell">
        <td colspan="2">
            Large Cell
        </td>
    </tr>
    <tr class="NextRow">
        <th>
            Header1
        </th>
        <td>
            <span class="valueIneed">val1</span>
        </td>
    </tr>
    <tr>
        <th>
            Header2
        </th>
        <td>
        <span class="valueIneed">val2</span>
    </td>
    </tr>
    <tr class="NextRow">
        <th>
            Header3
        </th>
        <td>
            <span class="valueIneed">val3</span>
        </td>
    </tr>
    <tr>
        <th>
            Header 4
        </th>
        <td>
            <span class="valueIneed">val4</span> 
        </td>
    </tr>
</table>

Open in new window

Comment
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE