2

I'm trying to figure out why this piece of code isn't working. It takes HTML from another part of the code, takes the table out of it, creates another column and tries to move a td element to the row above in the newly created column.

The table imported:

+-------------------+-------------------+-------------------+
| Existing column1  | Existing column2   | Existing column3 |
+-------------------+-------------------+-------------------+
| A                 | B                 | C                 |
| D                 | E                 | F                 |
| G                                                         |
+-------------------+-------------------+-------------------+

I want to try and make it look like this:

+-------------------+-------------------+-------------------+-------------+
| Existing column1  | Existing column2  | Existing column3  | New column1 |
+-------------------+-------------------+-------------------+-------------+
| A                 | B                 | C                 |             |
| D                 | E                 | F                 | G           |
+-------------------+-------------------+-------------------+-------------+

So whenever a td element has the class text-info, it moves it up a tr and appends it to the last Comment column

My code so far:

$dom = new DOMDocument();//Loads DOM document
$dom->loadHTML($str);//Loads HTML from a previously set variable
$xpath = new DOMXPath($dom);
$tables = $xpath->query('//table[@class="behaviourtable table"]');//Get only table from HTML
$commsTable = '';
foreach ($tables as $table) {
    $commsTable .=  $dom->saveXML($table);
}

$commsHTML = new DOMDocument();
$commsHTML->loadHTML($commsTable);

$tr = $commsHTML->getElementsByTagName('tr');


$th = $commsHTML->createElement('th', 'Comment');
$tr->item(0)->appendChild($th);

$xpathcomms = new DOMXPath($commsHTML);
$comments = $xpathcomms->query('//td[@class="text-info"]');
if($comments->length > 0){
    echo "if running";
foreach($comments as $comment){
    $parent = $comment->parentNode;
$parent->appendChild($comment);
$commsHTML->saveXML($parent);
}

}


echo $commsHTML->saveHTML();
4
  • I think $tables hasn't any result. Because class name can't contain space but you used [@class="behaviourtable table"] Commented Oct 12, 2016 at 16:11
  • @Mohammad The code works up until the $xpathcomms = new DOMXPath($commsHTML); line. The $tables, definitely returns the HTML table with the new column Comments. Commented Oct 12, 2016 at 16:15
  • Does php return any error? Commented Oct 12, 2016 at 16:21
  • @Mohammad No error is given by PHP Commented Oct 12, 2016 at 18:35

1 Answer 1

2
+50

In your code you append the td to it's original parent node (which does nothing), while what you are actually wanna do is get the parent (tr), go to it's previous sibling (prev tr) and append the td to that tr:

foreach($comments as $comment){
    $parent = $comment->parentNode;
    $parent->previousSibling->appendChild($comment);
}

Here is a complete working example:

$str = <<<END
<table class="behaviourtable table">
    <tr>
        <th>Existing column1</th>
        <th>Existing column2</th>
        <th>Existing column3</th>
    </tr><tr>
        <td>A</td>
        <td>B</td>
        <td>C</td>
    </tr><tr>
        <td>D</td>
        <td>E</td>
        <td>F</td>
    </tr><tr>
        <td class="text-info">G</td>
    </tr>
</table>
END;

$dom = new DOMDocument();//Loads DOM document
$dom->loadHTML($str);//Loads HTML from a previously set variable
$xpath = new DOMXPath($dom);
$tables = $xpath->query('//table[@class="behaviourtable table"]');//Get only table from HTML
$commsTable = '';
foreach ($tables as $table) {
    $commsTable .=  $dom->saveXML($table);
}

$commsHTML = new DOMDocument();
$commsHTML->loadHTML($commsTable);

$tr = $commsHTML->getElementsByTagName('tr');


$th = $commsHTML->createElement('th', 'Comment');
$tr->item(0)->appendChild($th);

$xpathcomms = new DOMXPath($commsHTML);
$comments = $xpathcomms->query('//td[@class="text-info"]');
if($comments->length > 0){
    foreach($comments as $comment){
        $parent = $comment->parentNode;
        $parent->previousSibling->appendChild($comment);
    }
}

echo $commsHTML->saveHTML();

Check this working example:
https://3v4l.org/FZkNE

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.