PHP - Need Help Ignoring Certain Items Acquired From A Foreach Loop
Basically I have the following code (posted below)
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 "<li type=square>" . $item_xml->itemInfo->item->attributes()->name . " "; echo "<b>(" . $item_xml->itemInfo->item->attributes()->level . ")</b></br>"; //calculate gear score total $calc=$calc+$item_xml->itemInfo->item->attributes()->level; } echo '</ul>'; //print the total item level echo '<b>Total Additive Item Score (Equipped):</b>' . $calc; echo '<br>'; //calculate the average item level(equipped) $ilvl=$calc/16; echo '<b>Average Item Score (Equipped):</b>' . round($ilvl) . "<br><br>"; It is working relatively well for what I am trying to accomplish. It pulls xml data from wowarmory and uses it to calculate a player's "average item level". The problem I am running in to is if a player is equipped with certain items (that should not be calculated), the average is incorrect. I know atleast the shirt, tabard and off-hand need to be ignored IF equipped. I am trying to find something relative to all 3 items in the xml data and thus tell the script to disregard them. Here is a printed example of the array I am working with (warning its LONG). This is an example of a player that has these 3 extra items. Shirt (Block 4), off-hand(shield) (Block 17), and Tabard (Block19) : Quote SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=59458 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_gizmo_newgoggles [id] => 59458 [level] => 359 [name] => Energized Bio-Optic Killshades [quality] => 4 [requiredSkill] => Engineering [requiredSkillRank] => 525 [type] => Mail ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 150956 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) [createdBy] => SimpleXMLElement Object ( [spell] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => trade_engineering [id] => 81720 [name] => Energized Bio-Optic Killshades ) [reagent] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 20 [icon] => inv_misc_pyriumbar [id] => 52186 [name] => Elementium Bar [quality] => 1 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 20 [icon] => inv_stone_15 [id] => 54849 [name] => Obsidium Bar [quality] => 1 ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 2 [icon] => inv_misc_truegold [id] => 58480 [name] => Truegold [quality] => 2 ) ) ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=67130 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [icon] => inv_misc_forestnecklace [id] => 67130 [level] => 359 [name] => Dorian's Lost Necklace [quality] => 4 [type] => Miscellaneous ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 53434 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=58129 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_shoulder_mail_dungeonmail_c_04 [id] => 58129 [level] => 346 [name] => Seafoam Mantle [quality] => 3 [type] => Mail ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 112607 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_largeshard_superior [id] => 52721 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Heavenly Shard [quality] => 3 [type] => Enchanting ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=14617 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_shirt_red_01 [id] => 14617 [level] => 1 [name] => Sawbones Shirt [quality] => 1 [type] => Miscellaneous ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 6250 ) ) [dropCreatures] => SimpleXMLElement Object ( [creature] => SimpleXMLElement Object ( [@attributes] => Array ( [area] => Scholomance [areaUrl] => fl[source]=dungeon&fl[dungeon]=scholomance&fl[boss]=all&fl[difficulty]=all [classification] => 1 [dropRate] => 2 [heroic] => 1 [id] => 11261 [maxLevel] => 42 [minLevel] => 42 [name] => Doctor Theolen Krastinov [title] => The Butcher [type] => Humanoid [url] => fl[source]=dungeon&fl[dungeon]=scholomance&fl[difficulty]=heroic&fl[boss]=11261 ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=56563 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [icon] => inv_chest_raidshaman_i_01 [id] => 56563 [level] => 359 [name] => Twilight Scale Chestguard [quality] => 4 [type] => Mail ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 101750 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) [createdBy] => SimpleXMLElement Object ( [spell] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => trade_leatherworking [id] => 78489 [name] => Twilight Scale Chestguard ) [reagent] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 10 [icon] => item_savageleather [id] => 52976 [name] => Savage Leather [quality] => 1 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_rubysanctum2 [id] => 52979 [name] => Blackened Dragonscale [quality] => 1 ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_pelt_12 [id] => 52980 [name] => Pristine Hide [quality] => 3 ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_volatilewater [id] => 52326 [name] => Volatile Water [quality] => 1 ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_volatilefire [id] => 52325 [name] => Volatile Fire [quality] => 1 ) ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_volatilelife_green [id] => 52329 [name] => Volatile Life [quality] => 1 ) ) ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=56538 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [icon] => inv_belt_mail_raidshaman_i_01 [id] => 56538 [level] => 359 [name] => Stormleather Sash [quality] => 4 [type] => Mail ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 53729 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) [createdBy] => SimpleXMLElement Object ( [spell] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => trade_leatherworking [id] => 78462 [name] => Stormleather Sash ) [reagent] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 10 [icon] => item_savageleather [id] => 52976 [name] => Savage Leather [quality] => 1 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_rubysanctum2 [id] => 52979 [name] => Blackened Dragonscale [quality] => 1 ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_pelt_12 [id] => 52980 [name] => Pristine Hide [quality] => 3 ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_volatilewater [id] => 52326 [name] => Volatile Water [quality] => 1 ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_volatilefire [id] => 52325 [name] => Volatile Fire [quality] => 1 ) ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_volatilelife_green [id] => 52329 [name] => Volatile Life [quality] => 1 ) ) ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=67148 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [icon] => inv_pant_raidshaman_i_01 [id] => 67148 [level] => 359 [name] => Kilt of Trollish Dreams [quality] => 4 [type] => Mail ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 196103 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=62363 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_boots_raidshaman_i_01 [id] => 62363 [level] => 359 [name] => Earthmender's Boots [quality] => 4 [type] => Mail ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 159778 ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=59310 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_bracer_raidshaman_i_01 [id] => 59310 [level] => 359 [name] => Chaos Beast Bracers [quality] => 4 [type] => Mail ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 102564 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) [dropCreatures] => SimpleXMLElement Object ( [creature] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [area] => Blackwing Descent [classification] => 3 [dropRate] => 3 [id] => 43296 [maxLevel] => 88 [minLevel] => 88 [name] => Chimaeron [type] => Beast ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [area] => Blackwing Descent [classification] => 3 [dropRate] => 2 [id] => 47774 [maxLevel] => 88 [minLevel] => 88 [name] => Chimaeron [type] => Beast ) ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=60314 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_glove_raidshaman_i_01 [id] => 60314 [level] => 359 [name] => Gloves of the Raging Elements [quality] => 4 [type] => Mail ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 105223 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) [dropCreatures] => SimpleXMLElement Object ( [creature] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [area] => Baradin Hold [classification] => 1 [dropRate] => 1 [id] => 47120 [maxLevel] => 88 [minLevel] => 88 [name] => Argaloth [type] => Demon ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [area] => Baradin Hold [classification] => 1 [dropRate] => 1 [heroic] => 1 [id] => 51350 [maxLevel] => 88 [minLevel] => 88 [name] => Argaloth [type] => Demon ) ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=59220 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_misc_rubystar [id] => 59220 [level] => 359 [name] => Security Measure Alpha [quality] => 4 [type] => Miscellaneous ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 53434 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) [dropCreatures] => SimpleXMLElement Object ( [creature] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [area] => Blackwing Descent [classification] => 1 [dropRate] => 3 [id] => 42180 [maxLevel] => 88 [minLevel] => 88 [name] => Toxitron [type] => Mechanical ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [area] => Blackwing Descent [classification] => 1 [dropRate] => 3 [id] => 49050 [maxLevel] => 88 [minLevel] => 88 [name] => Toxitron [type] => Mechanical ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [area] => Blackwing Descent [classification] => 1 [dropRate] => 2 [id] => 49056 [maxLevel] => 88 [minLevel] => 88 [name] => Arcanotron [type] => Mechanical ) ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=67129 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [icon] => inv_misc_pearlring1 [id] => 67129 [level] => 359 [name] => Signet of High Arcanist Savor [quality] => 4 [type] => Miscellaneous ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 53434 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=62465 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_misc_branch_01 [id] => 62465 [level] => 359 [name] => Stump of Time [quality] => 4 [type] => Miscellaneous ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 80507 ) ) [translationFor] => SimpleXMLElement Object ( [@attributes] => Array ( [factionEquiv] => 0 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_misc_branch_01 [id] => 62470 [level] => 359 [name] => Stump of Time [quality] => 4 ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=62047 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [icon] => inv_inscription_tarot_volcanocard [id] => 62047 [level] => 359 [name] => Darkmoon Card: Volcano [quality] => 4 [type] => Miscellaneous ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 107343 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=67131 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [icon] => inv_misc_cape_cataclysm_caster_c_01 [id] => 67131 [level] => 359 [name] => Ritssyn's Ruminous Drape [quality] => 4 [type] => Cloth ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 99205 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=61338 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_mace_1h_pvp400_c_01 [id] => 61338 [level] => 359 [name] => Vicious Gladiator's Gavel [quality] => 4 [type] => One-Handed Maces ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 334291 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=55070 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [icon] => inv_shield_bwdraid_d_02 [id] => 55070 [level] => 359 [name] => Elementium Stormshield [quality] => 4 [type] => Shields ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 115029 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) [createdBy] => SimpleXMLElement Object ( [spell] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => spell_shadow_sealofkings [id] => 76455 [name] => Elementium Stormshield ) [reagent] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 8 [icon] => inv_misc_ebonsteelbar [id] => 53039 [name] => Hardened Elementium Bar [quality] => 1 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [count] => 1 [icon] => inv_misc_pyriumbar [id] => 52186 [name] => Elementium Bar [quality] => 1 ) ) ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=64672 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_shield_56 [id] => 64672 [level] => 359 [name] => Relic of Norgannon [quality] => 4 [type] => Relic ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [sellPrice] => 101843 ) ) [disenchantLoot] => SimpleXMLElement Object ( [@attributes] => Array ( [requiredSkillRank] => 500 ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [canAuction] => 1 [dropRate] => 6 [icon] => inv_misc_crystalepic [id] => 52722 [level] => 85 [maxCount] => 1 [minCount] => 1 [name] => Maelstrom Crystal [quality] => 4 [type] => Enchanting ) ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [globalSearch] => 1 [lang] => en_us [requestQuery] => i=65904 [requestUrl] => /item-info.xml ) [itemInfo] => SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [@attributes] => Array ( [icon] => inv_misc_tabard_tolvir [id] => 65904 [level] => 85 [name] => Tabard of Ramkahen [quality] => 1 [type] => Miscellaneous ) [cost] => SimpleXMLElement Object ( [@attributes] => Array ( [buyPrice] => 10000 [sellPrice] => 2500 ) ) [vendors] => SimpleXMLElement Object ( [creature] => SimpleXMLElement Object ( [@attributes] => Array ( [area] => Uldum [classification] => 0 [heroic] => 1 [id] => 48617 [maxLevel] => 83 [minLevel] => 83 [name] => Blacksmith Abasi [title] => Ramkahen Quartermaster [type] => Humanoid ) ) ) ) ) ) I guess the shirt and tabard can be identified by the "[Quality]=>1" which no other items should typically have. The big issue comes with the third odd ball (off hand). This off-hand could be a shield or any weapon with few exceptions depending on the player class. As I continue to try and figure this out, one solution could be to first take the previous two "[Quality]=>1" items out of the equation IF they exist, then ONLY IF there are 17 items remaining subtract the value of block/iteration 17 before calulating as usual. If 16 items, then just run $calc is it currently is. How would I accomplish this? I do not mind displaying the item's name, in fact I would prefer to keep the item list as it currently I just need to not include the said items in the item level average. I know this is probably relatively straight forward to accomplish and that I am a noob, but would be grateful of your assistance. Thanks. Similar TutorialsHi I have an array of items that I am currently displaying a foreach loop What I'm looking to do, is add a string, to all of items, a part from the last 4: foreach ($pager->getResults() as $items => $item) { echo $item->getName(); //need to add a string to this for the all but the last 4 } Thanks Below is my output on the browser: Student: Kevin Smith (u0867587) Course: INFO101 - Bsc Information Communication Technology Course Mark 70 Grade Year: 3 Module: CHI2550 - Modern Database Applications Module Mark: 41 Mark Percentage: 68 Grade: B Session: AAB Session Mark: 72 Session Weight Contribution 20% Session: AAE Session Mark: 67 Session Weight Contribution 40% Module: CHI2513 - Systems Strategy Module Mark: 31 Mark Percentage: 62 Grade: B Session: AAD Session Mark: 61 Session Weight Contribution 50% Now where it says course mark above it says 70. This is incorrect as it should be 65 (The average between the module marks percentage should be 65 in the example above) but for some stange reason I can get the answer 65. I have a variable called $courseMark and that does the calculation. Now if the $courseMark is echo outside the where loop, then it will equal 65 but if it is put in while loop where I want the variable to be displayed, then it adds up to 70. Why does it do this. Below is the code: Code: [Select] $sessionMark = 0; $sessionWeight = 0; $courseMark = 0; $output = ""; $studentId = false; $courseId = false; $moduleId = false; while ($row = mysql_fetch_array($result)) { $sessionMark += round($row['Mark'] / 100 * $row['SessionWeight']); $sessionWeight += ($row['SessionWeight']); $courseMark = ($sessionMark / $sessionWeight * 100); if($studentId != $row['StudentUsername']) { //Student has changed $studentId = $row['StudentUsername']; $output .= "<p><strong>Student:</strong> {$row['StudentForename']} {$row['StudentSurname']} ({$row['StudentUsername']})\n"; } if($courseId != $row['CourseId']) { //Course has changed $courseId = $row['CourseId']; $output .= "<br><strong>Course:</strong> {$row['CourseId']} - {$row['CourseName']} <strong>Course Mark</strong>" round($courseMark) "<strong>Grade</strong> <br><strong>Year:</strong> {$row['Year']}</p>\n"; } if($moduleId != $row['ModuleId']) { //Module has changed if(isset($sessionsAry)) //Don't run function for first record { //Get output for last module and sessions $output .= outputModule($moduleId, $moduleName, $sessionsAry); } //Reset sessions data array and Set values for new module $sessionsAry = array(); $moduleId = $row['ModuleId']; $moduleName = $row['ModuleName']; } //Add session data to array for current module $sessionsAry[] = array('SessionId'=>$row['SessionId'], 'Mark'=>$row['Mark'], 'SessionWeight'=>$row['SessionWeight']); } //Get output for last module $output .= outputModule($moduleId, $moduleName, $sessionsAry); //Display the output echo $output; I think the problem is that it is outputting the answer of the calculation only for the first session mark. How in the while loop can I do it so it doesn't display it for the first mark only but for all the session marks so that it ends up showing the correct answer 65 and not 72? Hey guys, Got another question im hoping someone can help me with. I have a foreach loop (for use in a mysql query): foreach ($interests as $interest) { $query .= "($id, $interest), "; } problem is i do not want the comma(,) in the last loop. Is there some kinda of function i can use so it does not insert it on last loop? Or should i just use a for loop with a nested if loop? something like ; for($i=0; $i < count($interests); $i++){ $query .= "($id, '$interests[$i]')"; if($i + 1 < count($interests)) { $query .= ", "; } } Cheers guys Hi all, I have a table that shows data from an xml response that has been converted to an array. I have successfully managed to get that to work but it's showing more items than I want. I'm trying to isolate the items by state. There are 11 or so different states, of which I only want 7. I've tried the following with a while loop but I keep getting the message "undefined index: job_status" Here's my code (partial) $projects = array(); $xml=simplexml_load_string($response) or die("Error: Cannot create object"); foreach($xml->Jobs->Job as $item) { $projects[] = array( 'job_no' => (string)$item->ID, 'job_name' => (string)$item->Name, 'job_due' => (string)$item->DueDate, 'job_status' => (string)$item->State, 'job_staff' => (string)$item->Assigned->Staff->Name, ); } usort($projects, function($a,$b) {return $a['job_due'] <=> $b['job_due']; } ); $projects = array_reverse($projects); while ($projects['job_status'] == ('Feasibility') && ('Measure Up') && ('Model Drawing') && ('Concept Design') && ('Developed Design') && ('Resource Consent') && ('Construction Documentation')) { foreach ($projects as $proj) { $formatted = date('d/m/Y', strtotime($proj['job_due'])); $job_no ="{$proj['job_no']}"; $job_name ="{$proj['job_name']}"; $job_due ="$formatted"; $job_status ="{$proj['job_status']}"; $job_staff ="{$proj['job_staff']}"; Any help would be much appreciated thanks. I have a foreach loop which shows the breadcrumb display. But it's stopped working. For some reason the links are no longer links. They are just echoed on screen with no <a> tags. setting up array: $crumb_query = mysqli_query($link, "SELECT f.forum_id, f.forum_cat_id, f.forum_name, c.cat_id, c.cat_name FROM ".TBL_PREFIX."forums as f LEFT JOIN ".TBL_PREFIX."categories as c ON c.cat_id = f.forum_cat_id WHERE f.forum_id = '$forum_id' ") or die(mysqli_error($link)); $crumb_info = mysqli_fetch_array($crumb_query, MYSQLI_ASSOC); $crumbs = array("index.php" => "Board Index", "./view_category.php?cid={$crumb_info['cat_id']}" => $crumb_info['cat_name'], "./view_topics.php?fid=$forum_id" => $crumb_info['forum_name']); echo build_crumbs($crumbs); the function: function build_crumbs($crumbs) { foreach($crumbs as $key => $value) { $crumb_display = "<a href=\"{$key}\">{$value}</a>"; $crumb_display = implode(" - ", $crumbs); } return $crumb_display; } can anyone see where im going wrong? I am working on a photo gallery script in which I need to show 9 images at a time. Here is my script so far Code: [Select] <?php foreach($pictures->result() as $p): ?> <?php for($i = 1; $i <= 9; $i++) { ?> <div class="item"> <ul> <li><a href="images/gallery/<?=$p->category;?>/<?=$p->photo_name;?>" rel="example1" ><img src="images/gallery/<?=$p->category;?>/thumb_<?=$p->photo_name;?>" alt="#" /></a></li> </ul> </div><!-- end item --> <?php } ?> <?php endforeach; ?> At the moment the script is showing one image at a time 9 times and for every entry in my database. I have been battling this for a little while now. Thanks in advance for all of your help! how do I echo "blah" after the first loop in a foreach loop and the in the rest of them echo "crazy"? what im trying to do is echo all of the results from the database query but add bold to results with a pid of 0. here is my code: $list = ''; $query = $link->query("SELECT * FROM ".TBL_PREFIX."forums ORDER BY f_lid ASC"); $result = $query->fetchAll(); foreach($result as $key => $val) { if($result[$key]['f_pid'] == 0) { $list .= '<b>'.$result[$key]['f_name'].'</b><br />'; } $list .= $result[$key]['f_name'].'<br />'; } echo $list; this works fine but the ones with a pid of 0 are displayed twice. once as bold and then once normal like so: General General New Features Testing Testing Sandbox Bugs my question is how to prevent this. Does anybody know how to make the following code correct? real sorry but its just absolutely blowing my mind im awful with loops and have pretty much no clue what the hell im doing in honesty, any help would be great, Thanks Code: [Select] $addIDs_ary = array_map('intval', $_POST['chkInv']); $addIDs_ary = array_filter($addIDs_ary); $value = $addIDs_ary; //Check if hit_id is already in hitlist $query = mysql_query("SELECT * FROM hitlist WHERE player_id = '$playerID'"); $result = mysql_query($query); while ($result = mysql_fetch_assoc($result)) foreach ($result as $hit_id => $value){ if($result[$hit_id] == $value); $str = ""; } } else Hey guys, need help on some addition. foreach($login as $line_num => $line) { $login = explode(" ", htmlspecialchars(str_replace(" "," ",$line))); if(stristr($login[1], "\n")) $login[1] = substr($login[1], 0, strlen($login[1])-2); $account_data[$line_num] = array($login[0], $login[1]); $UserID = $login[0]; $AuthKey = $login[1]; $MobLink = "http://mobsters-fb-apache-dynamic-lb.playdom.com/mob_fb/"; $RefreshStat = file_get_contents($MobLink."refresh_stat_manual?user_id=".$UserID."&auth_key=".$AuthKey); $Cash = explode ("<cash>", $RefreshStat); $Cash = explode ("<", $Cash[1]); $Cash = $Cash[0]; $CashForEcho = "$".number_format($Cash[0]); echo $CashForEcho."<br/>"; echo "<br/><br/>"; } The variable $Cash will be dynamic for each user I put in the $login_file, and I need to add all of those values up. So say I put in 3 users within the $login_file, and the $Cash for user 1 was $3,000, the $cash for user 2 was $6,000 and the $cash for user 3 was $1,000, I want to be able to automatically add them all up, so it would echo '$10,000' at the end. Hope I've explained it for you guys to understand easily enough, if not let me know. Thanks For some reason I'm getting an invalid argument supplied for foreach error and I'm not sure why. Code: [Select] <?php // Include the database page require ('../inc/dbconfig.php'); if (isset($_POST['edit_event_lineup'])) { $event_items = $_POST['event_items']; $event_id = (int)$_POST['event_id']; foreach ($event_items as $items) { print_r($items); $segment_writer_id = (int)$_POST['segment_writer_id']; $introduction = mysqli_real_escape_string($dbc, $_POST['introduction']); $conclusion = mysqli_real_escape_string($dbc, $_POST['conclusion']); $segment_type = mysqli_real_escape_string($dbc, $_POST['segment_type']); $match_number = (int)$_POST['match_number']; $segment_number = (int)$_POST['segment_number']; $match_type_id = (int)$_POST['match_type_id']; $segment_title = mysqli_real_escape_string($dbc, $_POST['segment_title']); $match_type_id = mysqli_real_escape_string($dbc, $_POST['match_type_id']); $titles_id_list = mysqli_real_escape_string($dbc, $_POST['titles_id_list']); $stipulations_id_list = mysqli_real_escape_string($dbc, $_POST['stipulations_id_list']); $characters_id_list = mysqli_real_escape_string($dbc, $_POST['character_id_list']); $preview = mysqli_real_escape_string($dbc, $_POST['preview']); } $query = "INSERT INTO `event_segments` (event_id, segment_writer_id, segment_type, sort_order_preview, segment_title, preview, is_submitted_yes_or_no_id) VALUES ('".$event_id."', '".$match_writer_id."', '".$segment_type."' ,'".$sort_order_preview."','".$segment_title."','".$preview."',2)"; mysqli_query($dbc, $query); } ?> Hello gurus, I've been pounding my head against the wall on this one. It must be something simple I'm missing. I have a text field that posts ($_POST['case_ids']) a comma separated value such as 10067,10076 When I use explode to break it up it should make it an array. However when I use it in a foreach loop I get an invalid argument error. If I do a check for is_array() it fails the check. If I print the array I get: Code: [Select] Array ( [0] => 10067 [1] => 10076 ) So I thought maybe I get the error because the individual array elements don't have commas between them so I wrote this to put commas in between each element except the last: Code: [Select] $cases = array(); $numItems = count($case_ids);//get count of array $i = 0;//initialize counter foreach($case_ids as $key => $value){ if($i+1 != $numItems) { $cases[$key] = $value.','; }else{ $cases[$key] = $value; }//end if $i+1 $i++; } However this new array still gets the invalid argument error. Are these not valid arrays? Any help would be greatly appreciated. Thanks in advance, Twitch $array = array("1","2","3"); foreach ($array as $value){ echo $value; } Put its only echoing out 3, not the whole array. Hey Guys. I have a custom function that simply loops through an array and outputs the menu categories it finds in the array.
The foreach loop passes the function an array and the function returns the the value. The problem is that the function returns that last value.
Now I know some of you might say ... "Why don't you put the foreach inside the function and return the value when the foreach loop has been completed"
The reason why is because I to use that foreach loop through another function as well. I was thinking about using globals to get the return value. But just wanted to get your perspective on the best option here.
Please let me know what you guys think Thanks
User Defined Function
function displayMenuCategories($menu_category){ $display_menu =""; $category_id = $menu_category['menu_cat_id']; $display_menu .= "<div class='menu_cat'>{$menu_category['menu_cat_name']}</div><br>\n"; if(!empty($menu_category['menu_cat_desc'])) { $display_menu .= "<div class='menu_desc'>{$menu_category['menu_cat_desc']}</div>\n"; } $category_info = array("display" => $display_menu, "category_id" => $category_id ); return $category_info; }Foreach loop $left_menu_categories // Is the array that contains all the menu categories foreach($left_menu_categories as $left_menu_category){ $get_categories = displayMenuCategories($left_menu_category); } var_dump($get_categories); Edited by eldan88, 06 July 2014 - 05:01 PM. Hello all,
I have the following code (which obviously has unnecessary redundancy) and I would like to replace it with a ForEach loop in order to make the code shorter, and perhaps more efficient.
// validate input $valid = true; if (empty($store_name)) { $storeNameError = 'Please enter a Store Name'; $valid = false; } if (empty($item_description)) { $itemDdescriptionError = 'Please enter an Item Description'; $valid = false; } if (empty($qty_pkg)) { $qtyPkgError = 'Please enter Quantity or Package'; $valid = false; } if (empty($pkg_of)) { $pkgOfError = 'Please enter the Count/Quantity'; $valid = false; }At this point (being a newbie) I'm trying to wrap my head around the ForEach statement, but all I can achieve is to tie-my-knickers-in-a-knot. I know this is childs-play for some of you here, and I hope you can help me get a working ForEach statement. The following is the declaration of some related arrays (which potentially may be incorrect/inefficient as well): // Create an array of the fields we want to capture, along with their labels $fields = array( 'store_name'=>'Store Name', 'item_description'=>'Item Description', 'qty_pkg'=>'Qty / Pkg', 'pkg_of'=>'Pkg. Of', 'price'=>'Price', 'flyer_page'=>'Flyer Page #', 'limited_time_sale'=>'Limited Time Sale Days', 'nos_to_purchase'=>'Nos. to Purchase' ); // Create an array-type variable to keep track of validation errors $fieldsErrorMsgs = array( 'storeNameError'=>'Please enter a Store Name', 'itemDescriptionError'=>'Please enter an Item Description', 'qtyPkgError'=>'Please enter Qty or Pkg', 'pkgOfError'=>'Please enter the Qty', 'priceError'=>'Please enter the Price', 'flyerPageError'=>'Please enter the Flyer Page #', 'limitedTimeSaleError'=>'Please enter the Limited Time Sale Days', 'nosToPurchaseError'=>'Please enter the No(s). to Purchase' );And here's what I've tried...but there seems to be the following two problems with the ForEach portion of the code: 1) No errors are being displayed (inspite of the fact that I'm leaving all fields blank and clicking on the [Create] button 2) A row is being created (with all blank columns) $valid = true; $errors = array(); foreach($fields AS $field=>$label){ //The following line is using the ternary operator, it's basically a shorthand if/else assignment. $errors[$field] = (!isset($values['store_name']) || !strlen($values['store_name'])) ? $errors[$label] = 'Mandatory field, input required!' : NULL; if(!strlen($errors[$label])){ $valid = false; } } // insert data if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO shoplist (store_name,item_description,qty_pkg,pkg_of,price,flyer_page,limited_time_sale,nos_to_purchase,shopper1_buy_flag,shopper2_buy_flag,purchased_flag,purchase_later_flag) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $q = $pdo->prepare($sql); $q->execute(array($store_name,$item_description,$qty_pkg,$pkg_of,$price,$flyer_page,$limited_time_sale,$nos_to_purchase,$initialize_flag,$initialize_flag,$initialize_flag,$initialize_flag)); Database::disconnect(); header("Location: index.php"); }The following is the FORM portion of the code, which is where the logic for displaying the "error messages" is defiend <form class="form-horizontal" action="create.php" method="post"> <?php foreach($fields AS $field=>$label){ //Print the form element for the field. echo "<label>{$label}:<br>"; //If the field had an error, display it. if(isset($errors[$field])){ echo ' <span class="error">'.$errors[$field].'</span><br>'; } //Echo the actual input. If the form is being displayed with errors, we'll have a value to fill in from the user's previous submission. echo '<input type="text" name="'.$field.'"'; if(isset($values[$field])){ echo ' value="'.$values[$field].'"'; } echo '/></label>'; } ?> <div class="form-actions"> <button type="submit" class="btn btn-success">Create</button> <a class="btn" href="index.php">Back</a> </div> </form>Appreciate the help guys. Thanks foreach ($export as $group => $arrayOpts) { //echo $arrayOpts[0]["choice"]; This returns No /** * If the option hasn't been selected, leave that part out entirely **/ if (!empty($arrayOpts)) // if the entire options array isn't empty, we'll display { if ($arrayOpts[0]["choice"]!="0" || $arrayOpts[0]["choice"]!="No") { $pdf->Cell(40,10,$group,0,1); $pdf->ImprovedTable($header,$arrayOpts); $pdf->Ln(); } } } Unfortunately I can't show print_r's at the moment, the main issue is it's running those $pdf-> lines even if that variable is equal to No, I proved it because up above commented out returns No, but it still runs the pdf code. Any thoughts? Been staring at it a while, maybe it's something in my conditional statement? Thanks. I have the following form the just has one field. Code: [Select] <html> <head> <title>Post</title> </head> <body> <form action="worldpay_callback.php" method="post"> <input type="text" name="username" id="username" /> <input type="submit" name="submit" id="submit" value="send" /> </form> </body> </html> I am then posting the form to the following page that is meant to loop through all of the post values and save them to a text file. For some reason my foreach loop doesn't seem to be working as done of the post values are getting written to the text file. Can anyone see anything I'm doing wrong? Code: [Select] $myFile ="notifications.txt"; $fh = fopen($myFile, 'w+') or die("can't open file"); foreach($_POST as $k=>$v) { $data = 'POST: Key:'.$k.' - Value:'.$v ." \n"; fwrite($fh, $data); } fclose($fh); return "[OK]"; Thanks for any help I am using a foreach loop to get the results of an array. My problem is that I need the results combined into one string. How can I accomplish this? Object- Take array of IP addresses and add up all occurrences of a particular IP address. //$item is an array of IP addresses foreach ($item as $key => $unique_ip) { if($unique_ip == $ip) $a++; } echo $a; I get a blank page. What am I missing? hello why does my div move when i use a foreach loop ? if i use the code below i get the following i added a border style to the div so i can see that some are in and some are out: result 1 = not in a div result 2 = in a div result 3 = in a div etc then on the end i get a div with no result in it Code: [Select] function name(){ echo' <div id="divName">'; $a = Class::find_all(); foreach ($a as $ab){ $name = $ab->name; $dest = $ab->destination; <ul> <li><a href="index.php?pageID='.$dest.'">'.$name.'</a></li> </ul>'; } echo'</div>'; } thanks rick |