troubleshooting Question

simple html dom php accessing table/cell values

Avatar of weekapaug
weekapaug asked on
HTMLPHP* DOM
4 Comments1 Solution2745 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros