I’m trying to find an easier way to create <td> elements and inserting text values into them.
There has to be an easier way than creating a new variable for each <td> element, right? Perhaps making a class with some abstraction? Or maybe I’m doing it the wrong way?
At this rate creating 7 <td>s will require me to create 7 variables, 7 different createTextNodes for each of them and 14 more lines of appendChild().
That will be 28 lines for only 7 <td>s. This seems excessive to me. Not to mention there’s more than one <tr>in the table. Is there any way to shorten the amount of lines I can type to create something like:
<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
Short example code of what I’m doing:
$doc = new DOMDocument;
$table = $doc->createElement('table');
$doc->appendChild($table);
$tr1 = $doc->createElement('tr');
$table->appendChild($tr1);
$td1 = $doc->createElement('td');
$tr1->appendChild($td1);
$td1_1 = $doc->createElement('td');
$tr1->appendChild($td1_1);
$td1_2 = $doc->createElement('td');
$tr1->appendChild($td1_2);
$title1 = $doc->createTextNode('This is title #1');
$title2 = $doc->createTextNode('This is title #2');
$title3 = $doc->createTextNode('This is title #3');
$td1->appendChild($title1);
$td1_1->appendChild($title2);
$td1_2->appendChild($title3);
echo $doc->saveXML();
?>
You can add the text to the elements directly:
Apart from that, there isnt much to shorten in terms of method calls. DOM is a verbose API.