Hey there. My first post here!
Now, I've got this problem trying to solve some logic problem in a foreach loop, while it sounds simple - and I know I've done it before, I can't for the life of me remember how, or re-invent the method of which I did it.
What I'm trying to do is to read an external XML file, break it down, and while looping the important fields which holds the data I want to echo, I want to check if a certain condition is met, i.e. if there is a <character ...> child that has classId="1" just to create an example.
The problem is, doing this inside the loop will, if there are 100 <character ..> childs, loop the condition 100 times, breaking or halting the if statement inside the loop will halt/break the loop itself.
Just checking like this:
if($character['classId'] == 1)
{
..
} else
{
..
}
Won't work either as that checks if all the character fields contain it without having the intended effect.
I'm wondering if any of you guys have any idea how to slove this, been sitting at it for a few hours and I can't for the life of me remember how I did it the first time.
I'll include the code I've got so far, removing the checks that didn't work. Any help would be greatly appreciated!
// Create a basic grabber function..
function grabArmory($url)
{
// cURL Settings
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute!
$data = curl_exec($ch);
// .. And close!
curl_close($ch);
// Then give us the dataz...
return $data;
}
// The URL to grab.
//$url = 'http://eu.wowarmory.com/guild-info.xml?r=Neptulon&gn=Ascendance';
$url = 'http://eu.wowarmory.com/guild-info.xml?r=Doomhammer&gn=rise+against';
// Grab dat data and cache it!
$data = grabArmory($url);
// Then try and parse it
$xml = new SimpleXMLElement($data);
// Fetch member count and store it.
$memberCount = $xml->guildInfo->guild->members['memberCount'];
// Loop all the Guild's members
foreach($xml->guildInfo->guild->members->character as $member)
{
/*
Rank
0: Guildmaster
1: Officer
2: Raider
3: Member
4: Trial
5: Alt
6: Community
*/
// Sort out their Ranking first and foremost!
if($member['rank'] == 0) { $member['rank'] = 'Guildmaster'; }
elseif($member['rank'] == 1) { $member['rank'] = 'Officer'; }
elseif($member['rank'] == 2) { $member['rank'] = 'Raider'; }
elseif($member['rank'] == 3) { $member['rank'] = 'Member'; }
elseif($member['rank'] == 4) { $member['rank'] = 'Trialist'; }
elseif($member['rank'] == 5) { $member['rank'] = 'Alt'; }
elseif($member['rank'] == 6) { $member['rank'] = 'Community'; }
else { $member['rank'] == 'Unknown'; }
/*
Sort Genders, Races and Classes by their ID's.
0 = Male
1 = Female
1: Human 1: Warrior
2: Orc 2: Paladin
3: Dwarf 3: Hunter
4: Nightelf 4: Rogue
5: Undead 5: Priest
6: Tauren 6: Death Knight
7: Gnome 7: Shaman
8: Troll 8: Mage
10: Blood Elf 9: Warlock
11: Draenei 11: Druid
*/
if($member['classId'] == 1) {
// Warriors
} elseif($member['classId'] == 2) {
// Paladins
} elseif($member['classId'] == 3) {
// Hunters
} elseif($member['classId'] == 4) {
// Rogues
} elseif($member['classId'] == 5) {
// Priests
} elseif($member['classId'] == 6) {
// Death Knights
} elseif($member['classId'] == 7) {
// Shamans
} elseif($member['classId'] == {
// Mages
} elseif($member['classId'] == 9) {
// Warlocks{
} else {
// Druids
}
}