简体   繁体   中英

XML/php foreach loop not looping all data

It seems my foreach is looping the correct amount of times. However it's only populating the variables with content from the first loop.

I tried it 2 ways. but firstly this is the url to the XML feed http://wowfeeds.wipeitau.com/GuildActivity.php?location=EU&rn=shadowsong&gn=antheas&output=XML&callback= ? so you can see the structure.

the php codee issss

function GetAchievements(){
$achurl = "http://wowfeeds.wipeitau.com/GuildActivity.php?location=EU&rn=shadowsong&gn=antheas&limit=100&output=XML&callback=?"; 
$achxml = new SimpleXMLElement($achurl);
// Achievements
foreach ($achxml->ACTIVITYLIST->ACTIVITYITEM as $ach) {
$name = $ach['NAME'];
echo $name;
//$Achievments = "<p><img src='$achimg' /> <span class='red'>$achname</span> $achtext <span class='red'>$achobj</span></p>";
//echo $Achievments;
}

}

This seems to just return a blank.

However if I alter the code @ $name = $ach['NAME'] to = $name =

    function GetAchievements(){
    $achurl = "http://wowfeeds.wipeitau.com/GuildActivity.php?location=EU&rn=shadowsong&gn=antheas&limit=100&output=XML&callback=?"; 
    $achxml = new SimpleXMLElement($achurl);
    // Achievements
    foreach ($achxml->ACTIVITYLIST->ACTIVITYITEM as $ach) {
    $name = $achxml->ACTIVITYLIST->ACTIVITYITEM->NAME;
    echo $name;
    //$Achievments = "<p><img src='$achimg' /> <span class='red'>$achname</span> $achtext <span class='red'>$achobj</span></p>";
    //echo $Achievments;
    }
}

Then it simply repeats the first entry the same number of times as entries.

EG. Name. Name. Name. This been driving me mad for 2 hours now. Please help :(

simplexml object is not an array, you might need to consider like this

$url    = 'http://wowfeeds.wipeitau.com/GuildActivity.php?'.
          'location=EU&rn=shadowsong&gn=antheas&output=XML&callback=?';
$achxml = simplexml_load_file($url);
foreach ($achxml->ACTIVITYLIST->ACTIVITYITEM as $ach)
{
  $name = (string) $ach->NAME;
  echo $name, "\n";
}

output :

Ichex
Azraelka
Brechnor
Rougwar
Bromious
Ziini
Ryoden
Ashlynne
Snappidagg
Flökræ
Flökræ
Sevenfold
Ashlynne
Bonewing
Goldstroke
Flökræ
Worgin
Bromious
Renevatio
Ziini
Flökræ
Flökræ
Strollomiona
Thorban
Ichex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM