foreach循环中的PHP变量

时间:2011-07-14 21:49:54

标签: php xml variables loops foreach

我正在查询API并获取XML结果。我正在尝试将这些结果并将其粘贴到我根据其状态的表格中。每列都是不同的状态,因此需要进入各自的列。我遇到的问题是当我有多个以相同状态返回的项目时。我相信这打破了循环。我得到的一些项目根本没有出现,有些项目正在重复出现。有什么想法,我怎么能实现我想要的?谢谢!

更新示例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>

2 个答案:

答案 0 :(得分:0)

好的我认为问题是=运算符覆盖了以前的任何值。

这里有两个选项:首先是使用数组并使用$somevar[] .= somevalue语法,因此每个值都会添加到数组中,而不是覆盖先前的值。

第二个选项是预先声明所有变量,然后使用.=连接运算符,以便将新字符串附加到前一个字符串的末尾。

以下是我在您的情况下可能会做的事情:

<?php
var $defectGatheringReqs = array();
var $defectReadyForDev = array();
 . . .     
foreach($xmlDefect as $assetDefect){
 . . .             
    switch($defectStatus)
    {
        case "Gathering Requirements"):
             $defectGatheringReqs[] = $defectId;
             break;
        case "Ready for Development"):
             $defectReadyForDev[] = $defectId;
             break;
  . . . 
    }
?>
<table>
    <tr>
        <td>
            Gathering Requirements
        </td>
        <td>
            Ready for Development
 . . .
    </tr>
    <tr>
        <td>
            <?php foreach($defectGatheringReqs as $id) { ?>
                <div class='bugsItem'>
                    <?php echo $id; ?>
                </div>
            <?php } ?>
        </td>
 . . . 

答案 1 :(得分:0)

我非常感谢所有的帮助。我最终找到了一个与我的问题完全不同的解决方案。我现在正在对我的服务器上的一个PHP文件进行jQuery AJAX调用,该文件调用另一台服务器上的API(必须这样做,因为AJAX调用本身不能在不同的域之间进行) XML返回。

然后,我只是使用JavaScript / jQuery来解析XML并将其附加到正确的列。 jQuery使附加部分更容易! :)

注意:显然不能将此答案标记为2天的选择答案,但之后会这样做。

相关问题