This code is only appending the last integer in the for statement. I’m trying to append $root_text for each value in the for statement. Is $root_text supposed to be an array? I’m only appending 1 value with $root->appendChild($root_text)
The code:
<?php
$doc = new DOMDocument('1.0', 'iso-8859-1');
$root = $doc->createElement('test');
$doc->appendChild($root);
for($i = 1; $i <= 10; $i++) {
$root_text = $doc->createTextNode($i);
}
$root->appendChild($root_text);
print $doc->saveXML();
?>
You’re currently assigning a new value to
$root_texteach time through the loop, retaining (and ultimately appending) only the node from the final iteration. Why not simplyappendChilddirectly in the loop?