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();
$tableshasn't any result. Because class name can't contain space but you used[@class="behaviourtable table"]$xpathcomms = new DOMXPath($commsHTML);line. The$tables, definitely returns the HTML table with the new columnComments.