Jump to content

For Loop First Run Issue


DealerLeaf

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/219126-for-loop-first-run-issue/
Share on other sites

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().

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.