Forum Moderators: coopster

Message Too Old, No Replies

Number of occurrences of an XML element

writing a for loop to read recurring elements

         

Drunk N Japan

1:05 pm on May 25, 2007 (gmt 0)

10+ Year Member



Hey y'all, good morning.
I hate to bother anyone on a lovely Friday, but I am stuck and could use a fresh pair of brain cells. I am working on parsing a sports XML feed. for the most part I am done, I have pulled all of the pertinent info from the first occurrences of the element in the file. What I need to do now is write a loop (or several embedded loops) that executes = to the number of times there are recurring elements.

For example: if I have 10 occurrences of the <team-stat> element


$n = numberofoccurrences;
for (var i=0;i<$n;i++)
{
foreach.........
}

What I am having a problem with is finding the number of occurrences for a particular element.

I appreciate the help!

cmarshall

1:35 pm on May 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What language are you using?

Drunk N Japan

1:38 pm on May 25, 2007 (gmt 0)

10+ Year Member



My apologies,

I am using php5(which I really like), but I just started using it a few days ago.

Thanks

cmarshall

2:41 pm on May 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you using the PHP 5 DOM [us2.php.net]?

If so, this [us2.php.net] might be what you need.

Drunk N Japan

2:54 pm on May 25, 2007 (gmt 0)

10+ Year Member



I am using that to get the attributes of the elements,
foreach($doc->getElementsByTagName('season')->item(0)->attributes as $attribute){
if($attribute->name == "season"){
$season = $attribute->value;
echo $attribute->name . ": " . $attribute->value . "<br>";
}
}
but I am having trouble using

$j = 0;
//get the number of recurrences
foreach($doc->getElementsByTagName('cbk-roster')->item($j)){
$j ++;
}
echo $j;

to get the number off occurrences of the actual parent element. I am using this second piece to find out how many times to run the for loop to read in all the data.
I am also getting the following error about the second foreach loop.
Parse error: parse error, unexpected ')' in ..... the foreach loop.

been working on this for a couple of days to no avail.

Thanks for all the help!

cmarshall

2:59 pm on May 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe this is your problem:

foreach($doc->getElementsByTagName('cbk-roster')->item[red][b]($j)[/b][/red])

Try this instead:

foreach($doc->getElementsByTagName('cbk-roster')->item[red][b][$j] as $item[/b][/red])

I can't really tell what's going on. The syntax looks a bit odd.

Drunk N Japan

3:48 pm on May 25, 2007 (gmt 0)

10+ Year Member



OK, out of the frying pan and into the fire.
here is how I found the number of occurrences
$j = 0;
//get the number of recurrences
$rosters = $doc->getElementsByTagName('cbk-roster');
foreach($rosters as $roster){
$j ++;
}
echo $j;

Works like a charm

problem now that I have the number of teams that I have to loop through I have to loop through the number of players.
How can I loop through the player stats inside the school element?

I was thinking of using the item($j) to specify which specific element I want. Maybe adding to the first recurrence check

$j = 0;
$y = 0;
$rosters = $doc->getElementsByTagName('cbk-roster');
foreach($rosters as $roster){
$players= $doc->getElementsByTagName('cbk-player');
foreach($players as $player){
$y++;
}
$j ++;
}
echo $y."<br>";
echo $j."<br>";

but that is still going to read all occurrences of cbk-player I think.
How can I incorporate the occurrence of the parent element?

cmarshall

3:59 pm on May 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Won't count() [us2.php.net] work on this?

Drunk N Japan

5:50 pm on May 25, 2007 (gmt 0)

10+ Year Member



I tried using count to no avail.
I am able to find the number of parents and the total number of children
but I would like to find the number of children for parent(1), parent(2),...
I tried
//echo count ($doc->cbk-roster[$j]->cbk-player)."<br>";

but I got a
Parse error: parse error, unexpected '[' in

this is what I am using now.
//get the number of recurrences
$rosters = $doc->getElementsByTagName('cbk-roster');
foreach($rosters as $roster){
echo count ($doc->cbk-roster[$j]->cbk-player)."<br>";
$j ++;
}
$players= $doc->getElementsByTagName('cbk-player');
foreach($players as $player){
$y++;
}
echo $y."<br>";
echo $j."<br>";

I have been reading the php5 manual and it has been of some help but I am still stuck.

cmarshall

5:56 pm on May 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might consider asking this thread be moved the PHP Forum [webmasterworld.com], as this looks like a pure PHP problem, not an XML problem. There are some real PHP whizzes there who could probably straighten you out in 5 minutes.

Drunk N Japan

6:30 pm on May 25, 2007 (gmt 0)

10+ Year Member



How do I ask to move a thread?

Drunk N Japan

6:31 pm on May 25, 2007 (gmt 0)

10+ Year Member



I almost forgot,

THANK YOU VERY MUCH FOR YOUR HELP.

cmarshall

6:34 pm on May 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unfortunately, there is no moderator on this forum, but I'll put in a good word for you with jatar_k [webmasterworld.com], who is one of the PHP mods.

Good luck.

Oh, yeah... You're welcome.

Drunk N Japan

5:46 pm on May 29, 2007 (gmt 0)

10+ Year Member



As anyone can tell I am still in a bit of a pickle. Any help would be appreciated

jatar_k

12:07 am on May 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this is the one that is still giving you the parse error?

//get the number of recurrences
$rosters = $doc->getElementsByTagName('cbk-roster');
foreach($rosters as $roster){
echo count ($doc->cbk-roster[$j]->cbk-player)."<br>";
$j ++;
}
$players= $doc->getElementsByTagName('cbk-player');
foreach($players as $player){
$y++;
}
echo $y."<br>";
echo $j."<br>";

are you running through these and displaying them? or are you just getting counts?

have you looked at what is returned in each part? Does this "$doc->getElementsByTagName('cbk-player');" give you all of the cbk-player elements with no reference to the rosters?

Drunk N Japan

1:37 pm on May 30, 2007 (gmt 0)

10+ Year Member



That is definitely the problem area.
The getElementsByTagName('cbk-roster') segment is giving me the correct number of teams, but with the cbk-player outside of the roster I am only getting the total number of players in the file. Unfortunately the getElementsByTagName('cbk-player') is not giving me the player in reference to the roster.

This section of code is just the opening of the parser. I am doing foreach later to pull each individual attributes of the children. And all of the children only have attributes and single tags. I am able to pull each seperate data item but I am not able to loop the schools, and inside that, loop through the players. I am basically trying to count the schools and the number of players per school.

A sample of the xml is below. (edited for space and tos)

<sports-statistics>
<sports-rosters>
<season season="2006"/>
<cbk-rosters>
<cbk-roster>
<team-city city="Fresno State" alias="Fres"/>
<team-name name="Bulldogs"/>
<cbk-player>
<name first-name="First" last-name="Last"/>
</cbk-player>
<cbk-player>
<name first-name="First" last-name="Last"/>
</cbk-player>
<cbk-player>
<name first-name="First" last-name="Last"/>
</cbk-player>
</cbk-roster>
<cbk-roster>
<team-city city="Georgia" alias="UGa"/>
<team-name name="Bulldogs"/>
<cbk-player>
<name first-name="First" last-name="Last"/>
</cbk-player>
<cbk-player>
<name first-name="First" last-name="Last"/>
</cbk-player>
</cbk-rosters>
</sports-rosters>
</sports-statistics>

jatar_k

12:55 pm on May 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so it does have them grouped.

if you iterate through every element then you could count them. Though that seems like alot of work, depending on what you are doing and how long the file is.

Drunk N Japan

2:42 pm on May 31, 2007 (gmt 0)

10+ Year Member



It is a fairly long file, I cut out 98% of it. Part of the problem is that all the data is in attributes as opposed to being in between the tags. The other problem is being able to dynamically assign the number of times to loop through the players. I am successfully counting the schools and the total number of players in the file. It is the counting of the players per team that has me stumped.

What I want to do is read the school and first player then write the data for the first player to my database(mysql). then the second, third, etc. till the end of that particular schools player. Then I want to switch to the next school and do it again. I will institute various data verification functions and duplications checks throughout the program. (Those are no problem I have already struggled through them)

My biggest problem is knowing the number of players per school.

cmarshall

2:45 pm on May 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This could be done with a combo of XSLT and PHP, but that's sort of a "better mousetrap" solution. I would recommend trying to do it with pure PHP first.

Drunk N Japan

5:38 pm on May 31, 2007 (gmt 0)

10+ Year Member



I am trying to stick to strictly php if I can. The solution will probably incite a forehead smack and "why didn't I think of that" but the obvious answer is because I didn't think of it. This problem has really been bugging me and I have to find an answer. I appreciate any help.

jatar_k

6:28 pm on May 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is it a one time import? well, probably yearly.

just thinking it would be straight forward enough to walk the whole thing, dump it into your db and then do counts etc from there.

or is that what you are planning?

Drunk N Japan

8:15 pm on May 31, 2007 (gmt 0)

10+ Year Member



Unfortunately it is seasonal or more if there are roster changes. I will also be doing this for about twenty other scripts. Hence the dynamic aspect of it. This particular file has just 5 schools and 71 players but some of the other files are complete team and player stats, some of the others are game stats. Thats why I am looking to do it with a parent child for loop.

I love php5 I just haven't figured it all the way out yet.