Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with nested loop...

Status
Not open for further replies.
May 13, 2005
56
US
I have a piece of code that is extracting info from a XML feed, parsing it and displaying the data on a PHP page. The last piece of code diplays the array, but I seem to have an issue with it.

Here is the code snippit:

Code:
$roster='12';
$number='15';
$i=0;
while ($i < $roster) {
$fran = $userdata[$i]["fid"];
print "<b>Team: $fran</b><br>";
    $a=0;
    while ($a < $number) {
    $player[] = $userdata[$i]["id"][$a];
    print "Player: $player[$a] <br>";
    $a++;
    }
$i++;
}

Variables are as follows
$roster= A number that represents amount of teams
$number= Represents players per team
$userdata = Data that is put in an array from the XML feed
$fran = Individual franchises or teams
$player= players on each team

The Teams are being displayed properly, followed by the correct number of players also displayed properly for the first set. But when it gets past the first team, all the teams still display correctly, but the players still display the players from the first team.

It acts as if $i here -->$player[] = $userdata[$i]["id"][$a]; is always 0, but if I place a print $i in before this line, it properly displays the correct number in each group of players?
If I manually change this $i to 1,2,3 etc, all the players change to their expected values.

any ideas?

Thank you.

mike
 
This line is the culprit
Code:
print "Player: $player[$a] <br>";
should be
Code:
print "Player: ".$player[$a]." <br>";
 
You can simplify your code by using for() loops instead of while loops.

Your original code:
Code:
$roster='12';
$number='15';
$i=0;
while ($i < $roster) {
$fran = $userdata[$i]["fid"];
print "<b>Team: $fran</b><br>";
    $a=0;
    while ($a < $number) {
    $player[] = $userdata[$i]["id"][$a];
    print "Player: $player[$a] <br>";
    $a++;
    }
$i++;
}
Simplified code:
Code:
$roster=12; // why use strings when these are really numbers?
$number=15;
for ($i=0;$i<$roster;$i++) {
     $fran = $userdata[$i]["fid"];
     echo '<b>Team: ' . $fran . "</b><br>\n";
     for ($a=0;$a<$number;$a++) {
         $player[] = $userdata[$i]["id"][$a];
         echo 'Player: ' . $player[$a] . "<br>\n";
    }
}

I also like to terminate all of my generated HTML strings with carriage returns, since it makes reading generated code much easier for humans. :)

Ken
 
Thank you for the response.. I made that change, but it did not change my results.

Here is the whole script

Code:
<?php
$file = "[URL unfurl="true"]http://football.myfantasyleague.com/2004/export?L=42146&TYPE=rosters";[/URL]
$usercount = 0;
$matchupcount = 0;
$userdata = array();
$state = '';

function startElement ($parser,$name,$attrib){
global $usercount;
global $matchupcount;
global $userdata;
global $state;

switch ($name) {
case $name=="FRANCHISE" : {
$userdata[$matchupcount]["fid"] = $attrib["ID"];
break;
}
case $name=="PLAYER" : {
if ($usercount !== 9) {
$userdata[$matchupcount]["id"][] = $attrib["ID"];
}
break;
}

default : {$state=$name;break;}
}
}

function endElement ($parser,$name){
global $usercount;
global $matchupcount;
global $userdata;
global $state;
$state='';
if($name=="PLAYER") {$usercount++;}
if($name=="FRANCHISE") {$matchupcount++;}

}

function characterData ($parser, $data) {
global $usercount;
global $userdata;
global $state;
if (!$state) {return;}
}
$simpleparser = xml_parser_create('UTF-8');
xml_set_element_handler($simpleparser, "startElement", "endElement");
xml_set_character_data_handler($simpleparser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}

while (!feof($fp)) {
$data = fgets($fp);
if (!xml_parse($simpleparser, $data, feof($fp))) {
 die(xml_error_string(xml_get_error_code($simpleparser)));
}
}

$number='15';
$i=0;
while ($i < $matchupcount) {
$fran = $userdata[$i]["fid"];
print "<b>Team: $fran</b><br>";
    $a=0;
    while ($a < $number) {
    $player[] = $userdata[$i]["id"][$a];
    print "Player: ".$player[$a]." <br>";
    $a++;
    }
$i++;
}


xml_parser_free($simpleparser);
?>

and the output can be viewed here
But when I change this part of the code from
Code:
$number='15';
$i=0;
while ($i < $matchupcount) {
$fran = $userdata[$i]["fid"];
print "<b>Team: $fran</b><br>";
    $a=0;
    while ($a < $number) {
    $player[] = $userdata[$i]["id"][$a];
    print "Player: ".$player[$a]." <br>";
    $a++;
    }
$i++;
}

to:
Code:
for ($i=0;$i<$matchupcount; $i++) {
$fran = $userdata[$i]["fid"];
$player1 = $userdata[$i]["id"][0];
$player2 = $userdata[$i]["id"][1];
$player3 = $userdata[$i]["id"][2];
$player4 = $userdata[$i]["id"][3];
$player5 = $userdata[$i]["id"][4];
$player6 = $userdata[$i]["id"][5];
$player7 = $userdata[$i]["id"][6];
$player8 = $userdata[$i]["id"][7];
$player9 = $userdata[$i]["id"][8];
print "<b>Team: $fran</b><br>";
print "Player: $player1 <br>";
print "Player: $player2 <br>";
print "Player: $player3 <br>";
print "Player: $player4 <br>";
print "Player: $player5 <br>";
print "Player: $player6 <br>";
print "Player: $player7 <br>";
print "Player: $player8 <br>";
print "Player: $player9 <br>";
}

I can display the data as i want, as you can see right here

But, I dont want to build this as a static output as the $number can and will change...
 
Thanks for the code upgrade Ken, I appriciate it...

Also
>>$roster=12; // why use strings when these are really >>numbers?
I am doing this because these numbers will be different for different league id's (at the top of my code in the previous post to this).

Your code cleaned up the look and feel, but I still have the same results?

Any other ideas..

Thanks again

Mike

 
I got this one...

My array that I was building for $player[$a] was getting reset every time, I needed to set another $variable ($b)that went up one every time, but didnt get reset (like a was).

Here is the final code..

Code:
$b=0;
$number=15;
for ($i=0;$i<$roster;$i++) {
     $fran = $userdata[$i]["fid"];
     echo '<b>Team: ' . $fran . "</b><br>\n";
     for ($a=0;$a<$number;$a++) {
         $player[] = $userdata[$i]["id"][$a];
         echo 'Player: ' . $player[$b] . "<br>\n";
         $b++;
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top