php - Populate content of page after page has been produced in for-loop

I need to produce reports, using DOMDocument. Since the pages will vary ( between 3 to 30 pages) I want to create these pages using a for-loop (amount being defined by a variable). The producing of pages works fine.

As step 2 I need to populate the pages with content. The content will also be procuded by DOMDocument. Since I am using "$page" in the for-loop I assume it is natural behaviour that the defined node value only is added to the last for-loop result.

I added a "wanted result" knowing that I do not have the logic in place for getting that result.

Question:

Is the after-populating possible to be done with only DOMDocument or would I need another tool, e.g. Xpath.

<?php

$totalPages = 3;

$xml = new DomDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$html = $xml->createElement('html');
$xml->appendChild($html);

$wrapper = $xml->createElement('div');
$wrapper->setAttribute('class', 'wrapper');
$html->appendChild($wrapper);

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

  $page = $xml->createElement('div');

  $page->setAttribute('class', 'pages');

  // $page->nodeValue = 'Content...'; // Kept as reference. Move to below populating.

  $page->setAttribute(
    'id',
    'page-' . $i
  );

  $wrapper->appendChild($page);

}

// Populate pages with content.
$page->nodeValue = 'Content...';
$wrapper->appendChild($page);

// Save & print.
$xml->save("result.xml");
echo $xml->saveHTML();

Result

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <div class="wrapper">
    <div class="pages" id="page-1"/>
    <div class="pages" id="page-2"/>
    <div class="pages" id="page-3">Content...</div>
  </div>
</html>

Wanted result

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <div class="wrapper">
    <div class="pages" id="page-1"/>Content page-1</div>
    <div class="pages" id="page-2"/>Content page-2</div>
    <div class="pages" id="page-3">Content page-3</div>
  </div>
</html>

Answer

Solution:

You could store the references to the elements created by createElement, and afterwards use them to set values.

For example

$pages = [];
for ($i=1; $i <= $totalPages ; $i++) {
    $page = $xml->createElement('div');
    $pages[] = $page;

Then afterward you could do:

$pages[0]->nodeValue = 'Content page-1';

Php demo

Answer

Solution:

To fulfill the need of populating the pages later in DOMDocument, one can make use of dynamic variables. It means you create all your pages first and since it is part of loop you can construct as many pages as you want.

Having a variable name per each page, e.g. (page_1, page_2, etc) will let you select the page you wish to populate. The populating of pages can therefor be done outside the for-loop.

The source of populating can be of your choice, e.g DOMDocument, array, import from file.

<?php

$totalPages = 3;

$xml = new DomDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$html = $xml->createElement('html');
$xml->appendChild($html);

$wrapper = $xml->createElement('div');
$wrapper->setAttribute('class', 'wrapper');
$html->appendChild($wrapper);

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

  ${'page_'.$i} = $xml->createElement('div');

  ${'page_'.$i}->setAttribute('class', 'pages');

  ${'page_'.$i}->setAttribute(
    'id',
    'page-' . $i
  );

  $wrapper->appendChild(
    ${'page_'.$i}
  );

}

// Populate pages with content.

$page_1->nodeValue = 'Content-page-1';
$wrapper->appendChild($page_1);

$page_2->nodeValue = 'Content-page-2';
$wrapper->appendChild($page_2);

$page_3->nodeValue = 'Content-page-3';
$wrapper->appendChild($page_3);

// Save & print.
$xml->save("result.xml");
echo $xml->saveHTML();

Result

<html>
<div class="wrapper">
 <div class="pages" id="page-1">Content-page-1</div>
 <div class="pages" id="page-2">Content-page-2</div>
 <div class="pages" id="page-3">Content-page-3</div>
</div>
</html>

Source