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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.