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>
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';
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>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.