Change background according to frequency

Asked

Viewed 36 times

2

I am trying to make a table that receives the numbers coming from an SQL query and according to the frequency of each of them, change the background.

An example (taken from an Internet site):

inserir a descrição da imagem aqui

I get a array() as the following of the query (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
)

With that I tried the following:

<? 
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 of the 1 at the 25. As it is a lot of data I created a loop that did 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 be 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>

Still it’s not working. I’m traveling somewhere here in my logic or is there some other lean way of doing?

  • Where the frequency values come from?

  • What is the logic of assigning the red bands to the various frequencies ?

  • @Andersoncarloswoss what values? They have no values. Just set by me right after the foreach(), where I start all with 1. @Isac to demonstrate that the number is leaving many times.

  • 1

    But the background color will not vary according to the frequency? As it has no values?

  • You see, the values (numbers that came out in the contest) come from the database. That’s all. So I kind of "count" if the number X came out 1 time, 2 times, 3 times. Anyway, maybe my logic is wrong.

  • "to demonstrate that the number is coming out often" But the 25 seems to me less red than the 19 for example. I couldn’t understand the correlation between colors and numbers.

  • @Isac the image has no relation to my code, it was taken from a website on the Internet. It was just to illustrate. I edited the question with this information.

  • Frequencies are the values that come in num0, num1, etc. ?

  • @Isac what comes in num0, num1 etc, are the numbers drawn in the lottery. They are the drawn numbers of the day. They change with each draw. The numbers vary from 1 to 25.

  • @Andersoncarloswoss through what you commented, I had a light and I managed to resolve. Thank you very much!

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.