I am trying to make a leader classification table similar to the one on this website :
But the only problem I have that does not show me the total sum of points well, I do not calculate the sum well. The rest is fine the only problem I have is that.
This is my code with the query:
<?php
$sql = "SELECT
clasificacion_pilotos.id,
clasificacion_pilotos.fk_pilotos,
clasificacion_pilotos.au,
clasificacion_pilotos.ch,
clasificacion_pilotos.ba,
clasificacion_pilotos.ru,
clasificacion_pilotos.es,
clasificacion_pilotos.mo,
clasificacion_pilotos.ca,
clasificacion_pilotos.az,
clasificacion_pilotos.at,
clasificacion_pilotos.gb,
clasificacion_pilotos.hu,
clasificacion_pilotos.be,
clasificacion_pilotos.it,
clasificacion_pilotos.sg,
clasificacion_pilotos.ma,
clasificacion_pilotos.ja,
clasificacion_pilotos.usa,
clasificacion_pilotos.me,
clasificacion_pilotos.br,
clasificacion_pilotos.ab,
pilotos.nombresyapellidos FROM clasificacion_pilotos INNER JOIN pilotos ON
clasificacion_pilotos.fk_pilotos=pilotos.id
WHERE clasificacion_pilotos.fk_pilotos=pilotos.id ORDER BY id";
$consulta = $DB_con->prepare($sql);
$consulta->execute();
if($consulta->rowCount() > 0){
$i=1;
echo "<table class='striped responsive-table'>
<thead>
<tr>
<th>Pos.</th>
<th>Piloto</th>
<th>Tot.</th>
<th>AU</th>
<th>CH</th>
<th>BA</th>
<th>RU</th>
<th>ES</th>
<th>MO</th>
<th>CA</th>
<th>AZ</th>
<th>AT</th>
<th>GB</th>
<th>HU</th>
<th>BE</th>
<th>IT</th>
<th>SG</th>
<th>MA</th>
<th>JA</th>
<th>USA</th>
<th>ME</th>
<th>BR</th>
<th>AB</th>
</tr>
</thead>";
while ($linea = $consulta->fetch(PDO::FETCH_ASSOC)) {
$total = array_reduce(array_keys($linea), function($v, $k) use($linea) {
if ($k !== 'linea') {
return $v += $linea[$k];
}
});
?>
<tr>
<td><?php echo $i ?></td>
<td><?php echo $linea['nombresyapellidos']; ?></td>
<td><?php echo $total;?></td>
<td><?php echo $linea['au']; ?></td>
<td><?php echo $linea['ch']; ?></td>
<td><?php echo $linea['ba']; ?></td>
<td><?php echo $linea['ru']; ?></td>
<td><?php echo $linea['es']; ?></td>
<td><?php echo $linea['mo']; ?></td>
<td><?php echo $linea['ca']; ?></td>
<td><?php echo $linea['az']; ?></td>
<td><?php echo $linea['at']; ?></td>
<td><?php echo $linea['gb']; ?></td>
<td><?php echo $linea['hu']; ?></td>
<td><?php echo $linea['be']; ?></td>
<td><?php echo $linea['it']; ?></td>
<td><?php echo $linea['sg']; ?></td>
<td><?php echo $linea['ma']; ?></td>
<td><?php echo $linea['ja']; ?></td>
<td><?php echo $linea['usa']; ?></td>
<td><?php echo $linea['me']; ?></td>
<td><?php echo $linea['br']; ?></td>
<td><?php echo $linea['ab']; ?></td>
</tr>
<?php
$i++;
}
}else
echo "<div class='col s12 card-panel yellow darken-2 center'>
<h5 class='black-text text-darken-2 center CONDENSED LIGHT5'>¡ Advertencia:
No se ha encontrado ningún registro !</h5>
</div>";
?>
</table>
What is wrong? How can I solve it so that the sum is calculated correctly?