我得到了一個XML文件被PHP回顯並且所有的代碼都能正常工作,但是我的問題是在一個標記內(檢查和屬性是章節,它被回顯)不止一個標籤(例如,這就是被作爲呼應考試#),我的數據呼應了桌上,我希望它是這樣顯示的:PHP,XML,回聲時重複屬性的問題
|第1章|第2章|
| - 考試1 --- | - 考試1 --- |
| - 考試2 --- | - 考試2 --- |
|第3章|第4章|
| - 考試1 --- | - 考試1 --- |
但是我所得到的是這樣的:(我知道爲什麼雖然)
|第1章|
| - 查看1 --- |
|第1章|
| - 考試2 --- |
|第2章|
| - 查看1 --- |
它不斷重複標籤的屬性,我知道爲什麼,因爲我呼應,我有作爲表數據的變量標籤的屬性。所以問題不在於它爲什麼會發生,而是如何將其改變爲以我想要的方式顯示它。
我想也許有刪除的屬性,如果有一個以上的方式(我知道你可以刪除它,但我不知道如何一次顯示它並刪除等)
的代碼,我有是這樣的:
在XML
<maintag>
<exam chapter="Chapter 1">
<ex id="1">Exam 1</ex>
<ex id="2">Exam 2</ex>
<ex id="3">Exam 3</ex>
</exam>
<exam chapter="Chapter 2">
<ex id="4">Exam 1</ex>
<ex id="5">Exam 2</ex>
</exam>
<exam chapter="Chapter 3">
<ex id="6">Exam 1</ex>
</exam>
<exam chapter="Chapter 4">
<ex id="7">Exam 1</ex>
<ex id="8">Exam 2</ex>
<ex id="9">Exam 3</ex>
<ex id="10">Exam 4</ex>
</exam>
</maintag>
的PHP
<?php
$xml = DOMDocument::load('examdata.xml');
$xpath = new DOMXPath($xml);
$exams = $xpath->query('//exam/ex');
$istyle = 1; //This is to add gray after every other row
echo " <table cellpadding=\"0\" cellspacing=\"3\" border=\"0\" width=\"60%\" align=\"center\">
<tr id=\"center\" bgcolor=\"#999\">
<td>Book</td>
<td>Exam</td>
<td>ID</td>
<td>Student's Exam Status</td>
</tr>
";
foreach($exams as $exams2) {
$chapter = $exams2->parentNode->getAttribute('chapter');
$examid = $exams2->getAttribute('id');
$examname = $exams2->nodeValue;
if ($examid < 5) { //where it says 5 there goes a php variable where it has the queried user's exam number from the database, again this is all finished no need to change this.
$exstatus = "You already took this exam.";
}elseif ($examid == 5) {
$exstatus = "This is your exam (exam link)";
}elseif ($examid > 5) {
$exstatus = "You are not yet on this exam";
}
echo "<tr id=\"center\"";
if ($istyle % 2 == 0)
echo " bgcolor=\"#ccc\"";
echo ">
<td>$chapter</td>
<td>$examname</td>
<td>$examid</td>
<td>$exstatus</td>
</tr>";
$istyle++;
}
echo "
</table>";
?>
注意,表結構比我說我要它,我得到它上面的方式不同,我只是改變了它,因爲我不能離開它,它的樣子。
請注意,我想改變的是它說章的地方,我希望它在其下方顯示它,以顯示考試1及其下面的考試。在下一章(在本例中爲第2章)的下一章中,在考試1下面和考試2下面。然後在考試2下方創建另一個表格行,並放置另外兩個章節,並在其他考試下方。
請注意,考試不遵循模式,這是該文件的編輯版本,因爲其中有數百個,並且這些值與您在上面看到的不同。
上面的代碼工作,我只是想修改它,以便它可以滿足我的要求。
很高興您分享您的解決方案。我認爲論壇的等待時間是有意義的,否則就會被濫用。您可以撰寫問題並立即給出答案,收集評分。 – martinstoeckli