我正在查詢API並獲取XML結果。我正在嘗試將這些結果記錄下來,並根據他們的狀態將其粘貼到一張表格中。每列都是不同的狀態,所以他們需要進入各自的列。我遇到的問題是當我有多個項目返回相同的狀態。我相信這正在打破循環。我收到了一些沒有出現的項目和一些正在重複的項目。任何想法如何我可以實現我在找什麼?謝謝!!foreach循環中的PHP變量
更新與示例XML:
<?xml version="1.0" encoding="UTF-8"?>
<Assets total="6">
<Asset>
<Attribute name="ID">B-1001</Attribute>
<Attribute name="Title">Some Bug #1</Attribute>
<Attribute name="Status">In Progress</Attribute>
</Asset>
<Asset>
<Attribute name="ID">B-1002</Attribute>
<Attribute name="Title">Some Bug #2</Attribute>
<Attribute name="Status">Code Review</Attribute>
</Asset>
<Asset>
<Attribute name="ID">B-1003</Attribute>
<Attribute name="Title">Some Bug #3</Attribute>
<Attribute name="Status">Code Review</Attribute>
</Asset>
<Asset>
<Attribute name="ID">B-1004</Attribute>
<Attribute name="Title">Some Bug #4</Attribute>
<Attribute name="Status">Ready for Development</Attribute>
</Asset>
<Asset>
<Attribute name="ID">B-1005</Attribute>
<Attribute name="Title">Some Bug #5</Attribute>
<Attribute name="Status">In Progress</Attribute>
</Asset>
<Asset>
<Attribute name="ID">B-1006</Attribute>
<Attribute name="Title">Some Bug #6</Attribute>
<Attribute name="Status">In Progress</Attribute>
</Asset>
<?php
foreach($xmlDefect as $assetDefect){
// variables from defects XML
$defectId = $assetDefect->Attribute[0];
$defectTitle = $assetDefect->Attribute[1];
$defectStatus = $assetDefect->Attribute[2];
if($defectStatus == "Gathering Requirements"){
$defectGatheringReqs = "<div class='bugsItem'>" . $defectId . "</div>";
}else if($defectStatus == "Ready for Development"){
$defectReadyForDev = "<div class='bugsItem'>" . $defectId . "</div>";
}else if($defectStatus == "In Progress"){
$defectInProgress = "<div class='bugsItem'>" . $defectId . "</div>";
}else if($defectStatus == "Code Review"){
$defectAEReview = "<div class='bugsItem'>" . $defectId . "</div>";
}else if($defectStatus == "Code Complete"){
$defectCodeComplete = "<div class='bugsItem'>" . $defectId . "</div>";
}
}
?>
<table>
<tr>
<td>
Gathering Requirements
</td>
<td>
Ready for Development
</td>
<td>
In Progress
</td>
<td>
Code Review
</td>
<td>
Code Complete
</td>
</tr>
<tr>
<td>
<?php echo $defectGatheringReqs; ?>
</td>
<td>
<?php echo $defectReadyForDev; ?>
</td>
<td>
<?php echo $defectInProgress; ?>
</td>
<td>
<?php echo $defectAEReview; ?>
</td>
<td>
<?php echo $defectCodeComplete; ?>
</td>
</tr>
</table>
你可以發佈'$ xmlDefect'內容的例子嗎? –
您似乎在您的foreach循環中缺少結束標記。 – jisaacstone
基本上你需要重置循環開始處的輸出變量,否則它們仍然包含上一次循環(迭代)的值。 – hakre