Xdega Posted January 19, 2011 Share Posted January 19, 2011 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; Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted January 19, 2011 Share Posted January 19, 2011 You could do this instead: $calc = $calc + $item_xml->itemInfo->item->attributes()->level; Before the loop add: $calc = 0; Quote Link to comment Share on other sites More sharing options...
Xdega Posted January 19, 2011 Author Share Posted January 19, 2011 That seemed to have nailed it. Thank you much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.