在 for 循环中生成页面后填充页面内容
Populate content of page after page has been produced in for-loop
我需要使用 DOMDocument 生成报告。由于页面会有所不同(在 3 到 30 页之间),我想使用 for 循环创建这些页面(数量由变量定义)。页面的制作工作正常。
作为第 2 步,我需要用内容填充页面。内容也将由 DOMDocument 生成。由于我在 for 循环中使用“$page”,因此我假设定义的节点值仅添加到最后一个 for 循环结果中是自然行为。
我添加了一个 "wanted result" 知道我没有获得该结果的逻辑。
问题:
是否可以仅使用 DOMDocument 完成后填充,还是我需要其他工具,例如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();
结果
<?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>
想要的结果
<?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>
您可以存储对 createElement 创建的元素的引用,然后使用它们设置值。
例如
$pages = [];
for ($i=1; $i <= $totalPages ; $i++) {
$page = $xml->createElement('div');
$pages[] = $page;
然后你可以做:
$pages[0]->nodeValue = 'Content page-1';
为了满足稍后在 DOMDocument 中填充页面的需要,可以使用动态变量。这意味着您首先创建所有页面,因为它是循环的一部分,您可以根据需要构建任意数量的页面。
每个页面都有一个变量名称,例如(page_1、page_2 等)会让您 select 您希望填充的页面。因此,页面的填充可以在 for 循环之外完成。
您可以选择填充的来源,例如 DOMDocument、数组、从文件导入。
<?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();
结果
<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>
我需要使用 DOMDocument 生成报告。由于页面会有所不同(在 3 到 30 页之间),我想使用 for 循环创建这些页面(数量由变量定义)。页面的制作工作正常。
作为第 2 步,我需要用内容填充页面。内容也将由 DOMDocument 生成。由于我在 for 循环中使用“$page”,因此我假设定义的节点值仅添加到最后一个 for 循环结果中是自然行为。
我添加了一个 "wanted result" 知道我没有获得该结果的逻辑。
问题:
是否可以仅使用 DOMDocument 完成后填充,还是我需要其他工具,例如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();
结果
<?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>
想要的结果
<?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>
您可以存储对 createElement 创建的元素的引用,然后使用它们设置值。
例如
$pages = [];
for ($i=1; $i <= $totalPages ; $i++) {
$page = $xml->createElement('div');
$pages[] = $page;
然后你可以做:
$pages[0]->nodeValue = 'Content page-1';
为了满足稍后在 DOMDocument 中填充页面的需要,可以使用动态变量。这意味着您首先创建所有页面,因为它是循环的一部分,您可以根据需要构建任意数量的页面。
每个页面都有一个变量名称,例如(page_1、page_2 等)会让您 select 您希望填充的页面。因此,页面的填充可以在 for 循环之外完成。
您可以选择填充的来源,例如 DOMDocument、数组、从文件导入。
<?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();
结果
<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>