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

Link to comment
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().

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.