0

I have a table with thead and tbody - the first few rows are like so:

<table class="general player-list-table">
    <thead>
        <tr>
            <th width="20"></th>
            <th class="header-name">
                <a href="playerlist.aspx?dpt=0&srt=1">Name</a>
            </th>
            <th width="20"></th>
            <th width="20"></th>
            <th>
                <a href="playerlist.aspx?dpt=0&srt=2">Club</a>
            </th>
            <th>
                <a href="playerlist.aspx?dpt=0&srt=3">Price</a>
            </th>
            <th>
                <a href="playerlist.aspx?dpt=0&srt=4">Rating</a>
            </th>
            <th>
                <a href="playerlist.aspx?dpt=0&srt=5" title="Games Played">PLD</a>
            </th>
            <th>
                <a href="playerlist.aspx?dpt=0&srt=6" title="Goals">GLS</a>
            </th>
            <th>
                <a href="playerlist.aspx?dpt=0&srt=7" title="Assists">ASS</a>
            </th>
            <th>
                <a href="playerlist.aspx?dpt=0&srt=8" title="Clean Sheets">CS</a>
            </th>
            <th>
                <a href="playerlist.aspx?dpt=0&srt=9" title="Goals Against">GA</a>
            </th>
            <th class="highlight">
                <a href="playerlist.aspx?dpt=0&srt=10" title="Month Points">MTH</a>
            </th>
            <th class="highlight header-tot">
                <a href="playerlist.aspx?dpt=0&srt=11" title="Overall total">TOT</a>
            </th>
        </tr>
    </thead>
    <tbody>
        <tr class="on">
            <td class="first no-border">
                <div class="pos pos1"></div>
            </td>
            <td class="highlight">
                <a href="/Classic/Stats/Player/petr-cech.aspx">P Cech</a>
            </td>
            <td class="highlight"></td>
            <td>
                <div class="left club-tiny club-tiny-arsenal"></div>
            </td>
            <td>
                <a href="/Classic/Stats/Club/arsenal.aspx">ARS</a>
            </td>
            <td class="highlight">4.2</td>
            <td>
                <div class="chilli normal"></div>
            </td>
            <td>34</td>
            <td>0</td>
            <td>0</td>
            <td>16</td>
            <td>31</td>
            <td class="highlight">2</td>
            <td class="highlight">35</td>
        </tr>
        <tr>
            <td class="first no-border">
                <div class="pos pos1"></div>
            </td>
            <td class="highlight">
                <a href="/Classic/Stats/Player/david-ospina.aspx">D Ospina</a>
            </td>
            <td class="highlight"></td>
            <td>
                <div class="left club-tiny club-tiny-arsenal"></div>
            </td>
            <td>
                <a href="/Classic/Stats/Club/arsenal.aspx">ARS</a>
            </td>
            <td class="highlight">4.1</td>
            <td>
                <div class="chilli normal"></div>
            </td>
            <td>4</td>
            <td>0</td>
            <td>0</td>
            <td>2</td>
            <td>5</td>
            <td class="highlight">0</td>
            <td class="highlight">3</td>
        </tr>
        <tr class="on">
            <td class="first no-border">
                <div class="pos pos2"></div>
            </td>
            <td class="highlight">
                <a href="/Classic/Stats/Player/hector-bellerin.aspx">H Bellerin</a>
            </td>
            <td class="highlight"></td>
            <td>
                <div class="left club-tiny club-tiny-arsenal"></div>
            </td>
            <td>
                <a href="/Classic/Stats/Club/arsenal.aspx">ARS</a>
            </td>
            <td class="highlight">4.2</td>
            <td>
                <div class="chilli normal"></div>
            </td>
            <td>36</td>
            <td>1</td>
            <td>5</td>
            <td>18</td>
            <td>33</td>
            <td class="highlight">4</td>
            <td class="highlight">52</td>
        </tr>
        <tr>
            <td class="first no-border">
                <div class="pos pos2"></div>
            </td>
            <td class="highlight">
                <a href="/Classic/Stats/Player/calum-chambers.aspx">C Chambers</a>
            </td>
            <td class="highlight"></td>
            <td>
                <div class="left club-tiny club-tiny-arsenal"></div>
            </td>
            <td>
                <a href="/Classic/Stats/Club/arsenal.aspx">ARS</a>
            </td>
            <td class="highlight">4.1</td>
            <td>
                <div class="chilli normal"></div>
            </td>
            <td>4</td>
            <td>0</td>
            <td>0</td>
            <td>2</td>
            <td>3</td>
            <td class="highlight">0</td>
            <td class="highlight">5</td>
        </tr>
        <tr class="on">
            <td class="first no-border">
                <div class="pos pos2"></div>
            </td>
            <td class="highlight">
                <a href="/Classic/Stats/Player/kieran-gibbs.aspx">K Gibbs</a>
            </td>
            <td class="highlight"></td>
            <td>
                <div class="left club-tiny club-tiny-arsenal"></div>
            </td>
            <td>
                <a href="/Classic/Stats/Club/arsenal.aspx">ARS</a>
            </td>
            <td class="highlight">4.2</td>
            <td>
                <div class="chilli normal"></div>
            </td>
            <td>3</td>
            <td>1</td>
            <td>0</td>
            <td>1</td>
            <td>6</td>
            <td class="highlight">0</td>
            <td class="highlight">2</td>
        </tr>
        <tr>
            <td class="first no-border">
                <div class="pos pos2"></div>
            </td>
            <td class="highlight">
                <a href="/Classic/Stats/Player/nacho-monreal.aspx">N Monreal</a>
            </td>
            <td class="highlight"></td>
            <td>
                <div class="left club-tiny club-tiny-arsenal"></div>
            </td>
            <td>
                <a href="/Classic/Stats/Club/arsenal.aspx">ARS</a>
            </td>
            <td class="highlight">4.3</td>
            <td>
                <div class="chilli normal"></div>
            </td>
            <td>36</td>
            <td>0</td>
            <td>3</td>
            <td>17</td>
            <td>34</td>
            <td class="highlight">4</td>
            <td class="highlight">42</td>
        </tr>
        <tr class="on">
            <td class="first no-border">
                <div class="pos pos3"></div>
            </td>
            <td class="highlight">
                <a href="/Classic/Stats/Player/gabriel-armando-de-abreu.aspx">Gabriel</a>
            </td>
            <td class="highlight"></td>
            <td>
                <div class="left club-tiny club-tiny-arsenal"></div>
            </td>
            <td>
                <a href="/Classic/Stats/Club/arsenal.aspx">ARS</a>
            </td>
            <td class="highlight">4.2</td>
            <td>
                <div class="chilli normal"></div>
            </td>
            <td>19</td>
            <td>1</td>
            <td>0</td>
            <td>10</td>
            <td>18</td>
            <td class="highlight">2</td>
            <td class="highlight">24</td>
        </tr>
        <tr>
            <td class="first no-border">
                <div class="pos pos3"></div>
            </td>
            <td class="highlight">
                <a href="/Classic/Stats/Player/laurent-koscielny.aspx">L Koscielny</a>
            </td>
            <td class="highlight"></td>
            <td>
                <div class="left club-tiny club-tiny-arsenal"></div>
            </td>
            <td>
                <a href="/Classic/Stats/Club/arsenal.aspx">ARS</a>
            </td>
            <td class="highlight">4.5</td>
            <td>
                <div class="chilli normal"></div>
            </td>
            <td>32</td>
            <td>4</td>
            <td>0</td>
            <td>15</td>
            <td>31</td>
            <td class="highlight">2</td>
            <td class="highlight">43</td>
        </tr>
        <tr class="on">
            <td class="first no-border">
                <div class="pos pos3"></div>
            </td>
            <td class="highlight">
                <a href="/Classic/Stats/Player/per-mertesacker.aspx">P Mertesacker</a>
            </td>
            <td class="highlight">
                <div class="right playerstatus doubtful" title="Hamstring"></div>
            </td>
            <td>
                <div class="left club-tiny club-tiny-arsenal"></div>
            </td>
            <td>
                <a href="/Classic/Stats/Club/arsenal.aspx">ARS</a>
            </td>
            <td class="highlight">4.4</td>
            <td>
                <div class="chilli normal"></div>
            </td>
            <td>23</td>
            <td>0</td>
            <td>0</td>
            <td>9</td>
            <td>24</td>
            <td class="highlight">0</td>
            <td class="highlight">17</td>
        </tr>
        <!-- etc -->

I'm trying to iterate through each tbody > tr, outputting certain values from each cell, such as:

  • Nothing from the first cell
  • Plaintext of a element in second cell
  • Nothing in third and fourth cells
  • Plaintext of a element in fifth cell
  • Plaintext of sixth cell, etc.

In order to accomplish this, I'm using the simple_html_dom library.

My code looks like:

    foreach($dom->find("table.player-list-table tbody tr") as $row){
            $r["name"] = $row->find("td", 1)->find("a")->plaintext;
            $r["club"] = $row->find("td", 4)->find("a")->plaintext;
            $r["valu"] = $row->find("td", 5)->plaintext;

            print_r($r);
    }

However, this outputs:

PHP Fatal error: Uncaught Error: Call to a member function find() on null in /var/www/html/.../foo.php:28

Which points to the first line under foreach:

$r["name"] = $row->find("td", 1)->find("a")->plaintext;

How can I make this work?

Ben
  • 8,894
  • 7
  • 44
  • 80

1 Answers1

2

There are some problems with documentation maybe, First you need to add a class to tbody, because when you are finding rows using tbody tag it is also including thead's rows, that's why you are getting this error. tbody tag now changed to

<tbody class="tbody">

and foreach loop also changed to

foreach($dom->find("table.player-list-table .tbody tr") as $row){

Ok, now it is showing all rows in .tbody.

But another problem now, when I parse rows and call find function for a tag, it return an object of all a in current row, means if there are 3 a in a row then object will contain 3 objects for each a, you have to target 1st object and then call plaintext to get its content, now your code in foreach loop also changed to

$name = $row->find("td", 1)->find("a");
$r["name"] = $name[0]->plaintext;
$club = $row->find("td", 4)->find("a");
$r["club"] = $club[0]->plaintext;
$r["value"] = $row->find("td", 5)->plaintext;

I am using PHP version 5.3.10, so I have to use temporary variable to store object.

Also if you don't want to add class to tbody, you have to edit simple_html_dom.php, here is what to do with it https://stackoverflow.com/a/4062007/3303041

Hope this helps you solve your problem.

Community
  • 1
  • 1
Syed Talha Hai
  • 648
  • 7
  • 14
  • Thank you - I modified your code to make each pair into one line, e.g.: `$r["name"] = $row->find("td", 1)->find("a",0)->plaintext;` by specifying `"a",0)` I don't need to store it in a variable! – Ben May 25 '16 at 13:18
  • 2
    Ok great, I have never used it but it seems to be good tool for PHP, I will try to use it in upcoming projects :) – Syed Talha Hai May 25 '16 at 13:33