开发者

PHP variables inside foreach loop

开发者 https://www.devze.com 2023-03-20 15:59 出处:网络
I am querying an API and getting XML results back.I am trying to take those results and stick them into a table that i have based on their status.Each column is a different status so they need to go i

I am querying an API and getting XML results back. I am trying to take those results and stick them into a table that i have based on their status. Each column is a different status so they need to go in their respective columns. The problem i am running in to is when i have multiple items that are being returned with the same status. I believe this is breaking the loop. i am getting some items that are not appearing at all and some that are being duplicated. Any thoughts how i can acheive what i am looking for? Thanks!!

UPDATE with example 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>


OK I think the problem is the = operator is overriding any previous values.

You have two options here: first is to use an array and use $somevar[] .= somevalue syntax so each value is added to the array rather than overriding the previous value.

The second option is to declare all your variable beforehand and then use the .= concatenation operator so that the new string is appended to the end of the previous string.

Here is what I would probably do in your situation:

<?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>
 . . . 


I really appreciate all of the help. I ended up finding a completely different solution to my issue. I am now making a jQuery AJAX call out to a PHP file on my server that is calling out to an API on a different server (had to do it this way because AJAX calls themselves can't be made across different domains) that is getting the XML returned.

Then, i am just using JavaScript/jQuery to parse the XML and append it to the correct columns. jQuery makes the appending part much much easier! :)

NOTE: can't mark this answer as the chosen answer for 2 days apparently but will do so after that.

0

精彩评论

暂无评论...
验证码 换一张
取 消