PHP如何在ID中追加一个数组?

问题描述 投票:0回答:1

我想在我的页面中追加一个关联数组。我在网上找了很多 此处 但当我尝试他们的答案时,它复制我的页面,因为有很多值。

这是我的代码。

$reponse = $bdd->query('SELECT nom FROM `scores`');

while ($donnees=$reponse->fetch(PDO::FETCH_ASSOC)){
    foreach($donnees as $clef => $valeur){

        $html = file_get_contents('tableau.php');
        libxml_use_internal_errors(true);
        $doc = new DOMDocument();
        $doc->loadHTML($html);
        $descBox = $doc->getElementById('noms');
        $appended = $doc->createElement('li', $valeur);
        $descBox->appendChild($appended);
        echo $doc->saveHTML();

    }
};

我怎样才能做到不使我的页面被多次复制?

php append appendchild id
1个回答
1
投票

你必须只在以下部分进行循环 li 被创建和添加。

$html = file_get_contents('tableau.php');
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
$descBox = $doc->getElementById('noms');

$reponse = $bdd->query('SELECT nom FROM `scores`');
while ($donnees=$reponse->fetch(PDO::FETCH_ASSOC)){
    foreach($donnees as $clef => $valeur){
        $appended = $doc->createElement('li', $valeur);
        $descBox->appendChild($appended);

    }
}

echo $doc->saveHTML();


0
投票

好吧,在Pavel Musil的帮助下,我找到了一个答案,我的多出现的页面。

我已经把我的页面分成了3个文件。

第一个文件是:

<!DOCTYPE HTML>
<html lang="fr">
	<?php
		include("scores.php");
	?>
</html>

第二个是PHP代码。

	<?php
		try {
			$bdd=new PDO('mysql:host=localhost;dbname=scores','root','');
		}
		catch(Exception $e){
			die('erreur :'.$e->getMessage());
		};

		$bdd->query("SET NAMES UTF8");
		$reponse = $bdd->query('SELECT nom FROM `scores`');
		$html = file_get_contents('tableauHtml.html');
		$doc = new DOMDocument();
		$doc->loadHTML($html);
		$descBox = $doc->getElementById('noms');

		$reponse = $bdd->query('SELECT nom FROM `scores`');
		while ($donnees=$reponse->fetch(PDO::FETCH_ASSOC)){
		    foreach($donnees as $clef => $valeur){
		        $appended = $doc->createElement('li', $valeur);
		        $descBox->appendChild($appended);
		    }
		};

		echo $doc->saveHTML();
	?>

最后一个是HTML,我想插入我的LI 。

	<head>
    ...
	</head>

	<body>
		<div id='all'>
			<h1>Ze Kouiz</h1>
			<div id="noms">

			</div>
      ...
	</body>

它的工作原理

© www.soinside.com 2019 - 2024. All rights reserved.