I am getting data from my database. everything works but the foreach loop gets duplicate items. I am getting the team names as duplicates.
Picture:
I know it's the fault of this code because it's inside a foreach loop but i don't know how to fix this:
foreach($arrayTotals as $team=>$values){ foreach($values as $v){ echo "<tr class=\"" . $v['kaupunki_id'] . "\"><td>".$numerointi ."."; echo "<td>" . $team; $sum1=0; $sum2=0; $numerointi ++; }
My whole code:
$arrayTotals = array(); foreach ($db->query("SELECT pisteet_1, pisteet_2, nimi, team_id, pisteet.kaupunki_id FROM pisteet INNER JOIN joukkueet ON joukkueet.id=pisteet.team_id ORDER BY team_id ASC") as $joukkuenimi) { $arrayTotals[$joukkuenimi['nimi']][] = array('pisteet_1'=>$joukkuenimi['pisteet_1'],'pisteet_2'=>$joukkuenimi['pisteet_2'],'kaupunki_id'=>$joukkuenimi['kaupunki_id']); } //var_dump($arrayTotals); $numerointi =1; echo "<table class=\"zebra\">"; foreach($arrayTotals as $team=>$values){ foreach($values as $v){ echo "<tr class=\"" . $v['kaupunki_id'] . "\"><td>".$numerointi ."."; echo "<td>" . $team; $sum1=0; $sum2=0; $numerointi ++; } foreach($values as $v){ echo "<td class=\"pisteet\">" . $v['pisteet_1'] . "/" . $v['pisteet_2'] . "</td>"; $sum1 +=$v['pisteet_1']; $sum2 +=$v['pisteet_2']; } echo '<td class="summa">'.$sum1.'/'.$sum2."</td>"; echo "</tr>"; } echo '</table>';
My array if i enable:
echo "<pre>"; print_r($arrayTotals); Array ( [Itis/hki] => Array ( [0] => Array ( [pisteet_1] => 6 [pisteet_2] => 10 [kaupunki_id] => 1 ) [1] => Array ( [pisteet_1] => 3 [pisteet_2] => 10 [kaupunki_id] => 1 ) ) [Harju/Jyväskylä] => Array ( [0] => Array ( [pisteet_1] => 3 [pisteet_2] => 10 [kaupunki_id] => 5 ) [1] => Array ( [pisteet_1] => 4 [pisteet_2] => 10 [kaupunki_id] => 5 ) ) [Jojot] => Array ( [0] => Array ( [pisteet_1] => 6 [pisteet_2] => 10 [kaupunki_id] => 5 ) [1] => Array ( [pisteet_1] => 7 [pisteet_2] => 10 [kaupunki_id] => 5 ) ) )
inner join
byleft join
? Without having any example data or the schemas it's not possible to help.//var_dump($arrayTotals);
. What doesvar_dump($arrayTotals);
give you?