Jump to content

Looping through pages with repeater's and their fields


RyanJ
 Share

Recommended Posts

I am struggling with a simple concept at the moment and any direction is appreciated.

I have a set of players and each player has a repeater field which contains several other fields ie punches, kicks, ect.

On the individual player page/template,  Opponent, Punches and Kicks are all fields contained in the stats field which is a repeater. I have also totaled each field as shown below. Easy enough :)

Individual Player 1 Page

Opponent      Punches       Kicks

Player 2               3                4

Player 3               5                6

_________________________

Total                    8                10

However, now I want to display all players on one page with their total stats. What I have so far you wonder? (its clearly not correct)

$leaders = $pages->find("template=player");
  $total = 0;
    foreach ($leaders as $player) {
     echo "<tr>";
     echo "<td><a href='{$player->url}'>{$player->title}</a></td>";
      foreach ($player->stats as $stat) {
     echo "<td>$stat->punch</td>";
    }
     echo "</tr>";
   }

From the example code above, I get the below output. **Note that the 3 and 5 are punches from the previous diagram.

Player Name      Punches      Kicks
player 1                   3                 5
player 2

Player 3

But this is what I want it to show

Player Name      Total Punches      Total Kicks
player 1                   8                             10
player 2                   10                           17

Player 3                   5                             6                             

Link to comment
Share on other sites

Your code looks correct to me, other than that it's missing a kicks column. Is this the actual code that you are using? I'm guessing that your question is the same as mine: why are the punches and kicks columns blank for player 2 and 3? The answer isn't clear to me, as it appears they should be populated. It might be good for us to get a look at the actual code in use here. 

Link to comment
Share on other sites

Hi Ryan,

My question above unfortunately was unclear and I apologize. It was clear in my head, but often is harder to explain when typed. To sum it up, I wanted to keep track of each players stats to include their opponent, kicks and punches. Each player can have up to 10 opponents so what I did was create an opponent field, kicks field and punches field and put them all in a repeater field and assigned that to the player template. Then I wanted to show the total kicks and total punches for each player. This was easy to do on the player template. Getting the total amounts of Kicks and Punches was key.

Now, on a new "Leader" page, I wanted to loop through all players plus show their total kicks and total punches. Because I was using repeaters, there was no way for me to get the total kicks or total punches for each player. I would end up with something like so:

Player Name      Total Punches      Total Kicks
player 1                   8                             10
player 1                   4                             5
Player 2                   3                             5

Player 2                   6                             3

So what I did to solve this was create my first module (hooray for me) and added two hidden fields,  totalpunches and totalkicks to the player template. The module loops through the repeater field for each player adding the kicks and punches and storing their totals in the hidden fields. I then just pulled in the player and their total fields on the "Leader" template.

The moral of the story here is several things:

1. Creating the module was intimidating since php is murky, but OOP is like the black sea to me. After reading a few post, your tut here and having a look at the hello world module, I just dove in. I am glad I did.

2. I did not want to use repeaters because I was concerned if the site grew they would not scale well. Fortunately for me you came to the rescue again here

With all that said, I am sure there is a better way or it is possible without the module, but this way works as well.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...