user2894386
user2894386

Reputation: 69

Php weird calculation with for. Restarts every time

My idea was to create a row with divs. With all of them given the number of their place in the row. Now I've got this problem and I dont know how to fix it..

I got this:

<?php

$dom = new DOMDocument('1.0', 'utf-8');

for ($i = 1; $i < 5; $i++){

    $elementNumber = "Rownumber: " . $i;
    $element = $dom->createElement('div', $elementNumber);
    $dom->appendChild($element);

    echo $dom->saveXML();
}
?>

That puts out:

Rownumber: 1
Rownumber: 1
Rownumber: 2
Rownumber: 1
Rownumber: 2
Rownumber: 3
Rownumber: 1
Rownumber: 2
Rownumber: 3
Rownumber: 4

but I just want this:

Rownumber: 1
Rownumber: 2
Rownumber: 3
Rownumber: 4

Does anyone know what I'm doing wrong?

Thanks in advance!

Upvotes: 0

Views: 34

Answers (1)

Trenton Trama
Trenton Trama

Reputation: 4930

Move echo $dom->saveXML(); to the outside of the loop.

saveXML, without any arguments prints the entirety of the data held in $dom;

Upvotes: 2

Related Questions