I'm trying to make a table that gets the numbers coming from a SQL query and according to the frequency of each of them, change the background.
An example (taken from a website):
Igetaarray()
likethefollowingfromthequery(example):
Array([intConcurso]=>1599[dataConcurso]=>2017-12-15[num0]=>1[num1]=>3[num2]=>4[num3]=>5[num4]=>6[num5]=>8[num6]=>9[num7]=>10[num8]=>14[num9]=>18[num10]=>20[num11]=>21[num12]=>22[num13]=>23[num14]=>25)
WiththisItriedthefollowing:
<?if($f["num0"] == "1") {
if ($freq_1 == 1) {
$cor_1 = "#e7b5b5";
$freq_1++;
} elseif ($freq_1 == 2) {
$cor_1 = "#de8c8c";
$freq_1++;
} elseif ($freq_1 == 3) {
$cor_1 = "#d66363";
$freq_1++;
} elseif ($freq_1 == 4) {
$cor_1 = "#ef4a4a";
$freq_1++;
} else {
$cor_1 = "#db0000";
$freq_1++;
}
} else {
$cor_1 = "#ffffff";
$freq_1 = 1;
}
?>
<td style="background-color: <?= $cor_1 ?>; margin: 20px 0;"> 1 </td>
This for each number from 1 to 25 . As a lot of data I've created a loop that does this:
<?php
for ($p = 0; $p <= 14; $p++) { // $f['num0'] a $f['num14']
for ($i = 1; $i <= 25; ++$i) { // números 1 a 25
echo htmlspecialchars('<? ') . '<br>';
echo 'if ($f["num' . $p . '"] == "' . $i . '") {' . '<br>';
echo 'if ($freq_' . $i . ' == 1) {' . '<br>';
echo '$cor_' . $i . ' = "#e7b5b5";' . '<br>';
echo '$freq_' . $i . '++;' . '<br>';
echo '}' . '<br>';
echo 'elseif ($freq_' . $i . ' == 2) {' . '<br>';
echo '$cor_' . $i . ' = "#de8c8c";' . '<br>';
echo '$freq_' . $i . '++;' . '<br>';
echo '}' . '<br>';
echo 'elseif ($freq_' . $i . ' == 3) {' . '<br>';
echo '$cor_' . $i . ' = "#d66363";' . '<br>';
echo '$freq_' . $i . '++;' . '<br>';
echo '}' . '<br>';
echo 'elseif ($freq_' . $i . ' == 4) {' . '<br>';
echo '$cor_' . $i . ' = "#ef4a4a";' . '<br>';
echo '$freq_' . $i . '++;' . '<br>';
echo '} else {' . '<br>';
echo '$cor_' . $i . ' = "#db0000";' . '<br>';
echo '$freq_' . $i . '++;' . '<br>';
echo '}' . '<br>';
echo '} else {' . '<br>';
echo '$cor_' . $i . ' = "#ffffff";' . '<br>';
echo '$freq_' . $i . ' = 1;' . '<br>';
echo '}' . '<br>';
echo '?>' . '<br>';
echo htmlspecialchars('<td style="background-color: ') . htmlspecialchars('<?= ') . '$cor_' . $i . htmlspecialchars(' ?>') . ';' . htmlspecialchars(' margin: 20px 0;"> ' . $i . ' </td>');
echo '<br>';
}
}
?>
The full page would stay here:
<div class="row">
<div id="resultadoFiltro">
<div class="col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-filter"></i>
Frequência das dezenas
</div>
<div class="panel-body" style="overflow-x: scroll;">
<div class="row center">
<div class="col-sm-12" style="">
<table class="table table-striped table-hover">
<thead>
<tr>
<th class="center"> Conc </th>
<th class="center"> 1 </th>
<th class="center"> 2 </th>
<th class="center"> 3 </th>
<th class="center"> 4 </th>
<th class="center"> 5 </th>
<th class="center"> 6 </th>
<th class="center"> 7 </th>
<th class="center"> 8 </th>
<th class="center"> 8 </th>
<th class="center"> 10 </th>
<th class="center"> 11 </th>
<th class="center"> 12 </th>
<th class="center"> 13 </th>
<th class="center"> 14 </th>
<th class="center"> 15 </th>
<th class="center"> 16 </th>
<th class="center"> 17 </th>
<th class="center"> 18 </th>
<th class="center"> 19 </th>
<th class="center"> 20 </th>
<th class="center"> 21 </th>
<th class="center"> 22 </th>
<th class="center"> 23 </th>
<th class="center"> 24 </th>
<th class="center"> 25 </th>
</tr>
</thead>
<tbody>
<?php
foreach ($resultados_sorteios as $f) {
$f = array_unique($f);
$freq_1 = 1;
$freq_2 = 1;
$freq_3 = 1;
$freq_4 = 1;
$freq_5 = 1;
$freq_6 = 1;
$freq_7 = 1;
$freq_8 = 1;
$freq_9 = 1;
$freq_10 = 1;
$freq_11 = 1;
$freq_12 = 1;
$freq_13 = 1;
$freq_14 = 1;
$freq_15 = 1;
$freq_16 = 1;
$freq_17 = 1;
$freq_18 = 1;
$freq_19 = 1;
$freq_20 = 1;
$freq_21 = 1;
$freq_22 = 1;
$freq_23 = 1;
$freq_24 = 1;
$freq_25 = 1;
?>
<tr>
<td> <?= $f['intConcurso'] ?> </td>
<!-- Aqui fica o resultado do loop criado no código acima -->
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
It's still not working. Am I traveling somewhere here in my logic or is there some other leaner way to do it?