Jump to content

Trying to add values from an xml array


Xdega

Recommended Posts

Basically I am pulling data from an XML feed on wowarmory.com

Everything is working as far as getting the item names and even a list of the item levels.

What I am trying to do now is add together all of the item levels in to one value that I will call $calc (I will then divide this by 16 to find the average item level).

 

I am using a foreach loop to echo the Item Name and it's corresponding item level. I am now trying to grab the item level on each loop so I can add them all up and store them in $calc for further manipulation outside of the loop. I am a complete novice but learning, How would I go about achieving this?

 

Of course the code I have atm is re-defining $calc every loop, so naturally it isn't working.

Here is my current code:

//Get the UID for each item the character has
    //Then look up the name for that item
    foreach($char_xml->characterInfo->characterTab->items->item as $item)
    {
        //get the item id
        $item_id = $item->attributes()->id;
        //get the xml doc for that item from wow armory
        $url = "http://www.wowarmory.com/item-info.xml?i=" . $item_id;
        $data = fetchXML($url);
        //create a SimpleXML object to parse the xml
        $item_xml = new SimpleXmlElement($data);
        //print the item's name
        echo "<b> Item Name: </b>" . $item_xml->itemInfo->item->attributes()->name . "</br>";
	echo "<b><i> iLvL: </i></b>" . $item_xml->itemInfo->item->attributes()->level . "</br>";
	//obviously this is wrong, because It is redefining $calc every loop
	$calc=$item_xml->itemInfo->item->attributes()->level;
    }	
//print the total item level
echo $calc;

Link to comment
https://forums.phpfreaks.com/topic/225004-trying-to-add-values-from-an-xml-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.