2

Really thanks for the help guys. Even if you just find that everything was fine, you gave me important clues to solve it.

//SOLUTION Clear the first iteration process, as I was planning to do it all at begin of the page, and move it to main iteration. I don't know really what was going on, just have a clue about referencing the vars and then reuse it later on for a new iteration.

heres the working code:

foreach ($groups as $group) {                   
$group['games'] = db_getGamesElimbyGroup ($group['id']);
    $class_by_groupID = $group['id'];

foreach ($group['games'] as $game) {
   $game['players'] = db_getPlayerElimbyGames($game['id']);             
   $class_by_gameID = $game['id']; ?>
   <div class="group<?=$class_by_groupID?> game<?=$class_by_gameID?> item clearfix">

    <?  foreach ($game['players'] as $player) { ?>

            <div class="player box<?=$player['casa_fora']?> clearfix">
                <div class="thumb" style="background-image:url(<?=$home_path.$player['thumb']?>)"></div>
                <div class="name"><?=$player['nome']?></div>
                <div class="result"><?=$player['equipa_result']?></div>
            </div>

            <?  } ?>
       </div>

     <?}
 }

//END OF SOLUTION //NEW

Before get the array I make this operations with it:

$groups = db_getCountGroupsElim();

foreach ($groups as &$group) {

   foreach ($group['games'] as &$game) {
      $game['players'] = db_getPlayerElimbyGames($game['id']);
   }
}

Using referencing to add the result of the query to the same array. And if using the array as it is, or JSON form it works. I suspect it might have to be about using reference and the same variable name?!

//OLD

I got an array, with several levels, mainly they are Groups, Games, Players

Groups got Games and Games got Players.

Like so.

JSON

 [{"id":"9","name":"Oitavos","games":[{"id":"49","next_game_id":"58","players":[{"equipa_result":"0","casa_fora":"1","nome":"Lucas Neves Castro","thumb":"uploads\/1364040943\/thumb\/dsc01491.jpg","jogador_id":"2"},{"equipa_result":"1","casa_fora":"2","nome":"Jo\u00e3o Monteiro Ribeiro","thumb":"uploads\/1364249922\/thumb\/foto.png","jogador_id":"6"}]},{"id":"50","next_game_id":"58","players":[{"equipa_result":"1","casa_fora":"1","nome":"Lu\u00eds Salgado","thumb":"uploads\/1364229837\/thumb\/20130322171159.jpg","jogador_id":"1"},{"equipa_result":"0","casa_fora":"2","nome":"Pedro Fernandes","thumb":"uploads\/1364759964\/thumb\/sam_0255.jpg","jogador_id":"32"}]},{"id":"51","next_game_id":"59","players":[{"equipa_result":"1","casa_fora":"1","nome":"Pedro Costa","thumb":"uploads\/1364126670\/thumb\/pedro.jpg","jogador_id":"12"},{"equipa_result":"0","casa_fora":"2","nome":"Jos\u00e9 Vieira","thumb":"uploads\/1364134124\/thumb\/19084_540280705996807_1199242145_n.jpg","jogador_id":"13"}]},{"id":"52","next_game_id":"59","players":[{"equipa_result":"1","casa_fora":"1","nome":"Jos\u00e9 Peixoto","thumb":"uploads\/1364099591\/thumb\/foto.jpg","jogador_id":"10"},{"equipa_result":"0","casa_fora":"2","nome":"Pedro Sousa","thumb":"uploads\/1364150593\/thumb\/552121_566863856670835_1922080433_n.jpg","jogador_id":"16"}]},{"id":"53","next_game_id":"60","players":[{"equipa_result":"1","casa_fora":"1","nome":"Lu\u00eds Carvalho","thumb":"uploads\/1364150836\/thumb\/598590_119690038195872_1425120299_n.jpg","jogador_id":"17"},{"equipa_result":"0","casa_fora":"2","nome":"Jo\u00e3o Fernandes","thumb":"uploads\/1364171606\/thumb\/img_20130314_222526.jpg","jogador_id":"24"}]},{"id":"54","next_game_id":"60","players":[{"equipa_result":"1","casa_fora":"1","nome":"Patr\u00edcio Correia Santos","thumb":"uploads\/1364156207\/thumb\/dsc01877.jpg","jogador_id":"19"},{"equipa_result":"0","casa_fora":"2","nome":"Tiago Fernandes","thumb":"uploads\/1364171076\/thumb\/4232037_big.jpg","jogador_id":"23"}]},{"id":"55","next_game_id":"61","players":[{"equipa_result":"0","casa_fora":"1","nome":"Carlos Miguel Da Silva Lopes","thumb":"uploads\/1364217431\/thumb\/miguel1.jpg","jogador_id":"26"},{"equipa_result":"1","casa_fora":"2","nome":"Luis Silva","thumb":"uploads\/1364562821\/thumb\/177158_390926850971196_1987889821_o.jpg","jogador_id":"31"}]},{"id":"56","next_game_id":"61","players":[{"equipa_result":"0","casa_fora":"1","nome":"Tiago Teixeira Castro","thumb":"uploads\/1364220392\/thumb\/tiago.jpg","jogador_id":"27"},{"equipa_result":"1","casa_fora":"2","nome":"Hugo Pereira Castro","thumb":"uploads\/1364771622\/thumb\/263386_241332222564106_3344558_n.jpg","jogador_id":"29"}]}]},{"id":"10","name":"Quartos","games":[{"id":"58","next_game_id":"62","players":[{"equipa_result":"1","casa_fora":"1","nome":"Jo\u00e3o Monteiro Ribeiro","thumb":"uploads\/1364249922\/thumb\/foto.png","jogador_id":"6"},{"equipa_result":"0","casa_fora":"2","nome":"Lu\u00eds Salgado","thumb":"uploads\/1364229837\/thumb\/20130322171159.jpg","jogador_id":"1"}]},{"id":"59","next_game_id":"63","players":[{"equipa_result":"0","casa_fora":"1","nome":"Pedro Costa","thumb":"uploads\/1364126670\/thumb\/pedro.jpg","jogador_id":"12"},{"equipa_result":"1","casa_fora":"2","nome":"Jos\u00e9 Peixoto","thumb":"uploads\/1364099591\/thumb\/foto.jpg","jogador_id":"10"}]},{"id":"60","next_game_id":"62","players":[{"equipa_result":"1","casa_fora":"1","nome":"Patr\u00edcio Correia Santos","thumb":"uploads\/1364156207\/thumb\/dsc01877.jpg","jogador_id":"19"},{"equipa_result":"0","casa_fora":"2","nome":"Lu\u00eds Carvalho","thumb":"uploads\/1364150836\/thumb\/598590_119690038195872_1425120299_n.jpg","jogador_id":"17"}]},{"id":"61","next_game_id":"63","players":[{"equipa_result":"1","casa_fora":"1","nome":"Luis Silva","thumb":"uploads\/1364562821\/thumb\/177158_390926850971196_1987889821_o.jpg","jogador_id":"31"},{"equipa_result":"0","casa_fora":"2","nome":"Hugo Pereira Castro","thumb":"uploads\/1364771622\/thumb\/263386_241332222564106_3344558_n.jpg","jogador_id":"29"}]}]},{"id":"11","name":"Meias","games":[{"id":"62","next_game_id":"65","players":[{"equipa_result":"1","casa_fora":"1","nome":"Jo\u00e3o Monteiro Ribeiro","thumb":"uploads\/1364249922\/thumb\/foto.png","jogador_id":"6"},{"equipa_result":"0","casa_fora":"2","nome":"Patr\u00edcio Correia Santos","thumb":"uploads\/1364156207\/thumb\/dsc01877.jpg","jogador_id":"19"}]},{"id":"63","next_game_id":"65","players":[{"equipa_result":"0","casa_fora":"1","nome":"Jos\u00e9 Peixoto","thumb":"uploads\/1364099591\/thumb\/foto.jpg","jogador_id":"10"},{"equipa_result":"1","casa_fora":"2","nome":"Luis Silva","thumb":"uploads\/1364562821\/thumb\/177158_390926850971196_1987889821_o.jpg","jogador_id":"31"}]}]},{"id":"12","name":"3\u00ba e 4\u00ba","games":[{"id":"64","next_game_id":"0","players":[{"equipa_result":"0","casa_fora":"1","nome":"Patr\u00edcio Correia Santos","thumb":"uploads\/1364156207\/thumb\/dsc01877.jpg","jogador_id":"19"},{"equipa_result":"0","casa_fora":"2","nome":"Jos\u00e9 Peixoto","thumb":"uploads\/1364099591\/thumb\/foto.jpg","jogador_id":"10"}]}]},{"id":"13","name":"Final","games":[{"id":"65","next_game_id":"0","players":[{"equipa_result":"0","casa_fora":"1","nome":"Jo\u00e3o Monteiro Ribeiro","thumb":"uploads\/1364249922\/thumb\/foto.png","jogador_id":"6"},{"equipa_result":"0","casa_fora":"2","nome":"Luis Silva","thumb":"uploads\/1364562821\/thumb\/177158_390926850971196_1987889821_o.jpg","jogador_id":"31"}]}]}]

SAMPLE ARRAY

Array
(
[id] => 13
[name] => Final
[games] => Array
    (
        [0] => Array
            (
                [id] => 65
                [next_game_id] => 0
                [players] => Array
                    (
                        [0] => Array
                            (
                                [equipa_result] => 0
                                [casa_fora] => 1
                                [nome] => João Monteiro Ribeiro
                                [thumb] => uploads/1364249922/thumb/foto.png
                                [jogador_id] => 6
                            )

                        [1] => Array
                            (
                                [equipa_result] => 0
                                [casa_fora] => 2
                                [nome] => Luis Silva
                                [thumb] => uploads/1364562821/thumb/177158_390926850971196_1987889821_o.jpg
                                [jogador_id] => 31
                            )
                    )
            )
    )
)

And I'm doing a foreach for every single level, to create individual elements that can be styled easier with css.

My iterations are this:

foreach ($groups as $group) {                   

    $class_by_groupID = $group['id'];

foreach ($group['games'] as $game) {

   $class_by_gameID = $game['id']; ?>
   <div class="group<?=$class_by_groupID?> game<?=$class_by_gameID?> item clearfix">

    <?  foreach ($game['players'] as $player) { ?>

            <div class="player box<?=$player['casa_fora']?> clearfix">
                <div class="thumb" style="background-image:url(<?=$home_path.$player['thumb']?>)"></div>
                <div class="name"><?=$player['nome']?></div>
                <div class="result"><?=$player['equipa_result']?></div>
            </div>

            <?  } ?>
       </div>

     <?}
 }

Ok, so now that all the important code is here, I'm getting a weird result on the last output of the last game of the last group.

It's not because of the specific group, I tried to modify the array to get less results and still, the same last duplicate output.

So my last DIV, instead of gain the data of the last record, it's getting the previous one, and be duplicated in two HTML DIVS, just the same, but duplicated.

Any tip, or advice will be very welcome.

Thanks in advance.

GFFR88PT
  • 23
  • 4

3 Answers3

12

The foreach with reference was my 1st thought, but since your original code didn't show it I didn't mentioned it.

For future reference, in PHP's foreach function documentation you can read:

Warning

Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

Here's an example on how to workaround the problem:

<?php
    $arr = array(1, 2, 3, 4);
    foreach ($arr as &$value) {
        $value = $value * 2;
    }
    // $arr is now array(2, 4, 6, 8)
    unset($value); // break the reference with the last element
Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43
0

You might be getting a wired result, but all I can tell you is that this is not because of what you've posted in your question:

<?php

$groups[] = array(
    'id'    => 13,
    'name'  => 'Final',
    'games' => array(
        array(
            'id'           => 65,
            'next_game_id' => 0,
            'players'      => array(
                array('equipa_result' => 0, 'casa_fora' => 1, 'nome' => 'João Monteiro Ribeiro', 'thumb' => 'uploads/1364249922/thumb/foto.png', 'jogador_id' => 6),
                array('equipa_result' => 0, 'casa_fora' => 2, 'nome' => 'Luis Silva', 'thumb' => 'uploads/1364562821/thumb/177158_390926850971196_1987889821_o.jpg', 'jogador_id' => 31),
            ),
        ),
    ),
);

$home_path = '';

foreach ($groups as $group) {

    $class_by_groupID = $group['id'];

    foreach ($group['games'] as $game) {

        $class_by_gameID = $game['id']; ?>
        <div class="group<?= $class_by_groupID ?> game<?= $class_by_gameID ?> item clearfix">

            <?php foreach ($game['players'] as $player) { ?>

                <div class="player box<?= $player['casa_fora'] ?> clearfix">
                    <div class="thumb" style="background-image:url(<?= $home_path . $player['thumb'] ?>)"></div>
                    <div class="name"><?= $player['nome'] ?></div>
                    <div class="result"><?= $player['equipa_result'] ?></div>
                </div>

            <?php } ?>
        </div>

    <?php
    } // foreach
}

That code just runs fine and outputs:

<div class="group13 game65 item clearfix">


        <div class="player box1 clearfix">
            <div class="thumb" style="background-image:url(uploads/1364249922/thumb/foto.png)"></div>
            <div class="name">João Monteiro Ribeiro</div>
            <div class="result">0</div>
        </div>


        <div class="player box2 clearfix">
            <div class="thumb" style="background-image:url(uploads/1364562821/thumb/177158_390926850971196_1987889821_o.jpg)"></div>
            <div class="name">Luis Silva</div>
            <div class="result">0</div>
        </div>

            </div>

So where-ever you did made a mistake, you made it not in the code / data you've prodivded in your question. Anyway a hint: do not use short-tags, not when you illustrate your code here on this webiste. See as well: Are PHP short tags acceptable to use?.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Hi @hakre, thanks for the help. The thing is, that is just one of 5 groups, and one of 24 games. The full JSON data was posted, may you please give it a try?! Thanks! – GFFR88PT Apr 18 '13 at 23:14
0

Just looking... the code looks fine but you might have an extra record in your $groups array that is causing one extra iteration of the second loop.

CubCouper
  • 46
  • 1
  • 7