我使用以下代码将HTML模板的结构解析为DOM选项,然后使用SQL查询的结果,我试图生成并将某些html片段附加到dom对象的父节点,然后回显用样式生成的HTML。但是,呈现的HTML是原始HTML,并且没有被设置样式。
有关该怎么做的任何想法?我正在考虑将这一切都转移到JQuery而不是PHP,因为谷歌搜索后我找不到一个直接的解决方案,无需在HTML本身使用<script>
标签进行一些后期处理。
以下是应从中添加节点的模板HTML(jumbotron HTML):
<!-- CONTENT -->
<div class="container">
<div class="row">
<!--CONTENT DIVs-->
<div class="jumbotron col-xs-12 col-sm-10 col-sm-push-2" id="contents">
<!--END OF CONTENT DIVs-->
这是PHP代码:
//Create DOM of the main page
$masterdoc = new DOMDocument();
@$masterdoc->loadHTMLFile($filename);
$masterflag = $masterdoc->getElementById("contents");
//The SQL statement needed
$sql = "SELECT folder_name FROM FOLDERS INNER JOIN LINKAGES ON FOLDERS.id = LINKAGES.child WHERE folder_id = (SELECT id from FOLDERS WHERE folder_name = '" . $foldername . "');";
$result = $conn->query($sql);
$count = $result->num_rows;
//Save all the folder names into an array
$data = array();
$index = 0;
if ($count> 0) {
while($row = $result->fetch_array()) {
$data[$index] = $row["folder_name"];
$index++;
}
} else {
echo "0 results";
}
//Loop through the array to generate Jumbotrons and append these to the right section of the main HTML.
for ($x = 0; $x <count($data); $x++) {
$masterflag->appendChild($masterdoc->createTextNode('<div class="row"><div class="col-xs-2"><i class="fa fa-folder fa-4x"></i></div><div class="col-xs-8"><a href="http://localhost/www/folders.php?link="' . $data[$x] . '" class="btn btn-block" type="content" id="content">' . $data[$x] . '</a></div><div class="col-xs-2"><i class="fa fa-arrow-right fa-4x"></i></div></div><hr>'));
}
echo $masterdoc->saveHTML();
这样你就可以看到发生了什么,这是我的页面:
{{3}}
答案 0 :(得分:0)
请参阅this回答。您需要使用DomDocument::createDocumentFragment()
,然后将XML字符串附加到创建的片段,然后将DomElement::appendChild()
该片段附加到$masterflag