if you do this -
a majority of this code will go away.
assuming i decoded what you are trying to produce as output. the following should be all the code that you need for the data table section -
// pre-fetch the data, indexing/pivoting it using the station, then fuel as indexes
$data = [];
while ($row = sqlsrv_fetch_array($result))
{
$data[$row['station']][$row['fuel']] = $row;
}
// produce the output
?>
<table class = "main">
<tr class="main"><td></td>
<?php
// output the heading
foreach($fuel as $fu)
{
echo "<td class = 'main'>$fu</td>";
}
?>
<td class = "main">Atnaujinta</td></tr>
<?php
// output the data
foreach($station as $st)
{
echo "<tr><td class='main'><a href='levelStation.php?station=$st'><b>$st</b></a></td>";
foreach($fuel as $fu)
{
if(empty($data[$st][$fu]['vol']))
{
echo '<td>-</td>';
}
else
{
$row = $data[$st][$fu];
$class = '';
if ($row['fmm']<=100)
$class = 'class="low"';
if ($row['fmm']>100 AND $row['fmm']<250)
$class = 'class="alert"';
if ($row['vmm']>=5)
$class = 'class="water"';
$qs = [];
$qs['station'] = $st;
$qs['fuel'] = $fu;
$qs['type'] = 'all';
$qs = http_build_query($qs);
echo "<td $class><a href='levelFuel.php?$qs'>{$row['vol']}</a></td>";
}
}
echo "<td>".date_format(date_create($row['date']),"Y-m-d H:i:s")."</td></tr>\n";
}
// output the totals
?>
<tr><td>Iš viso:</td>
<?php
foreach($fuel as $fu)
{
$sum = 0;
foreach($station as $st)
{
$sum += $data[$st][$fu]['vol']??0;
}
echo "<td class = 'main'>$sum</td>";
}
?>
<td></td></tr>
</table>