DealerLeaf Posted November 18, 2010 Share Posted November 18, 2010 The first run of my for loop does not execute, but all after that works. I know I need to define my $strings before the for loop because they do not take effect until it has ran once and that is why it does not show the first run. But I don't completely understand how to solve the issue, and have tried everything to my knowledge. The code below imports a steam group page xml and gets the unique ids for each of the groups members, then retrieves information about members. <?php $groupxml = simplexml_load_file('http://steamcommunity.com/groups/MDK-Society/memberslistxml/?xml=1'); $members = $groupxml->memberCount; $array = $groupxml->members->steamID64; for($arraycount="0"; $arraycount<$members; $arraycount=$arraycount+1){ $xml = simplexml_load_file("http://steamcommunity.com/profiles/$array[$arraycount]/?xml=1"); echo "<a href=\"#\" title=\"<img src=$xml->avatarMedium align=right /> $xml->steamID<br />$xml->stateMessage<br /><br />Location: $xml->location<br />$xml->summary\"><img src=\"$xml->avatarMedium\" /></a>"; echo "<br>"; if ("$xml->onlineState" == "online") { echo "<img src=\"images/stateonlinemaster.png\" />"; } elseif ("$xml->onlineState" == "offline") { echo "<img src=\"images/stateofflinemaster.png\" />"; } else { echo "<img src=\"images/stateingamemaster.png\" />"; } echo "<br>"; echo "<br>"; } ?> Could you explain to me fully where I went wrong in the for loop. Thanks Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 18, 2010 Share Posted November 18, 2010 I'm not sure yet, but why not just use foreach(). It's much easier and is what it is designed for. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 18, 2010 Share Posted November 18, 2010 Without testing it, my suspicion is that since you define $arraycount as a string "0" and numeric string indexes don't work in arrays, then that is the first run problem. The second time through when 1 is added, $arraycount is cast to an integer and so it now works. But I would use foreach(). Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 18, 2010 Share Posted November 18, 2010 If you still want to use a "for" loop, here's how I would write it: <?php for($arraycount=0; $arraycount<$members; ++$arraycount){ ?> Ken Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 18, 2010 Share Posted November 18, 2010 Without testing it, my suspicion is that since you define $arraycount as a string "0" and numeric string indexes don't work in arrays, then that is the first run problem. The second time through when 1 is added, $arraycount is cast to an integer and so it now works. But I would use foreach(). So I did test it and I was partly wrong. You can access an integer index by specifying a numeric string, you just can't define numeric string indexes. Quote Link to comment Share on other sites More sharing options...
DealerLeaf Posted November 18, 2010 Author Share Posted November 18, 2010 I was playing with the foreach when I refreshed and checked kens post. Thank you both, Ken what exactly was wrong? the quotes or using ++ to add one instead? 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.