How to create summation row at table end

Asked

Viewed 85 times

0

I have a page with a table that shows information on the number of items expected to be performed and the number of items that were actually performed at the end of each month.

If the quantity of items predicted is greater than that of items made the cell turns red, otherwise it turns green.

I would like to make at the end of this table a row that shows the total values of each column, as if it were a record.

inserir a descrição da imagem aqui This is the code of the table:

<?php

                            require 'conn.php';

                            //Conexão e consulta ao Mysql
                            $qry = mysqli_query($lnk, "select * from qualite");

                            //Pegando os nomes dos campos
                            $num_fields = mysqli_num_fields($qry);//Obtém o número de campos do resultado

                            for($i = 0;$i<$num_fields; $i++){//Pega o nome dos campos
                                $fields[] = mysqli_fetch_field_direct($qry,$i)->name;
                            }

                            //Montando o cabeçalho da tabela
                            $table = '<table class="table table-hover table-inverse"> <tr style="">';

                            for($i = 0;$i < $num_fields; $i++){
                                $table .= '<th>'.$fields[$i].'</th>';
                            }

                            //Montando o corpo da tabela
                            //Tá meio gambiarrado mas pelo menos funciona.
                            $table .= '<tbody>';
                            while($r = mysqli_fetch_array($qry)){
                                $table .= '<tr>';
                                if($r['ID'] > $r['ID']){
                                  $table .= '<td>'.$r['ID'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['ID'].'</td>';
                                }
                                if($r['KPI'] > $r['KPI']){
                                  $table .= '<td style="font-weight:bolder;">'.$r['KPI'].'</td>';
                                }else{
                                  $table .= '<td style="font-weight:bolder;">'.$r['KPI'].'</td>';
                                }
                                if($r['PILOTE'] > $r['PILOTE']){
                                  $table .= '<td>'.$r['PILOTE'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['PILOTE'].'</td>';
                                }

                                //Aqui começam os meses
                                if($r['JAN_PREV'] > $r['JAN_REAL']){
                                  $table .= '<td>'.$r['JAN_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['JAN_REAL'].'</td>';
                                  $jan_prev .=+ $r['JAN_PREV'];
                                  $jan_real .=+ $r['JAN_REAL'];
                                }else{
                                  $table .= '<td>'.$r['JAN_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['JAN_REAL'].'</td>';
                                  $jan_prev .=+ $r['JAN_PREV'];
                                  $jan_real .=+ $r['JAN_REAL'];
                                }
                                if($r['FEV_PREV'] > $r['FEV_REAL']){
                                  $table .= '<td>'.$r['FEV_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['FEV_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['FEV_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['FEV_REAL'].'</td>';
                                }
                                if($r['MAR_PREV'] > $r['MAR_REAL']){
                                  $table .= '<td>'.$r['MAR_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['MAR_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['MAR_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['MAR_REAL'].'</td>';
                                }
                                if($r['ABR_PREV'] > $r['ABR_REAL']){
                                  $table .= '<td>'.$r['ABR_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['ABR_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['ABR_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['ABR_REAL'].'</td>';
                                }
                                if($r['MAI_PREV'] > $r['MAI_REAL']){
                                  $table .= '<td>'.$r['MAI_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['MAI_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['MAI_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['MAI_REAL'].'</td>';
                                }
                                if($r['JUN_PREV'] > $r['JUN_REAL']){
                                  $table .= '<td>'.$r['JUN_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['JUN_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['JUN_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['JUN_REAL'].'</td>';
                                }
                                if($r['JUL_PREV'] > $r['JUL_REAL']){
                                  $table .= '<td>'.$r['JUL_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['JUL_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['JUL_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['JUL_REAL'].'</td>';
                                }
                                if($r['AGO_PREV'] > $r['AGO_REAL']){
                                  $table .= '<td>'.$r['AGO_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['AGO_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['AGO_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['AGO_REAL'].'</td>';
                                }
                                if($r['SET_PREV'] > $r['SET_REAL']){
                                  $table .= '<td>'.$r['SET_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['SET_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['SET_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['SET_REAL'].'</td>';
                                }
                                if($r['OUT_PREV'] > $r['OUT_REAL']){
                                  $table .= '<td>'.$r['OUT_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['OUT_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['OUT_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['OUT_REAL'].'</td>';
                                }
                                if($r['NOV_PREV'] > $r['NOV_REAL']){
                                  $table .= '<td>'.$r['NOV_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['NOV_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['NOV_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['NOV_REAL'].'</td>';
                                }
                                if($r['DEZ_PREV'] > $r['DEZ_REAL']){
                                  $table .= '<td>'.$r['DEZ_PREV'].'</td>';
                                  $table .= '<td style="background:red;">'.$r['DEZ_REAL'].'</td>';
                                }else{
                                  $table .= '<td>'.$r['DEZ_PREV'].'</td>';
                                  $table .= '<td style="background:green;">'.$r['DEZ_REAL'].'</td>';
                                }



                                // Adicionando botão de edição

                                $table .= '<td><form action="qualite-edicao.php" method="post">'; 
                                $table .= '<input type="hidden" name="ID" value="'.$r['ID'].'">';
                                $table .= '<input type="hidden" name="KPI" value="'.$r['KPI'].'">';
                                $table .= '<input type="hidden" name="PILOTE" value="'.$r['PILOTE'].'">';
                                $table .= '<input type="hidden" name="JAN PREV" value="'.$r['JAN_PREV'].'">';
                                $table .= '<input type="hidden" name="JAN REAL" value="'.$r['JAN_REAL'].'">';
                                $table .= '<input type="hidden" name="FEV PREV" value="'.$r['FEV_PREV'].'">';
                                $table .= '<input type="hidden" name="FEV REAL" value="'.$r['FEV_REAL'].'">';
                                $table .= '<input type="hidden" name="MAR PREV" value="'.$r['MAR_PREV'].'">';
                                $table .= '<input type="hidden" name="MAR REAL" value="'.$r['MAR_REAL'].'">';
                                $table .= '<input type="hidden" name="ABR PREV" value="'.$r['ABR_PREV'].'">';
                                $table .= '<input type="hidden" name="ABR REAL" value="'.$r['ABR_REAL'].'">';
                                $table .= '<input type="hidden" name="MAI PREV" value="'.$r['MAI_PREV'].'">';
                                $table .= '<input type="hidden" name="MAI REAL" value="'.$r['MAI_REAL'].'">';
                                $table .= '<input type="hidden" name="JUN PREV" value="'.$r['JUN_PREV'].'">';
                                $table .= '<input type="hidden" name="JUN REAL" value="'.$r['JUN_REAL'].'">';
                                $table .= '<input type="hidden" name="JUL PREV" value="'.$r['JUL_PREV'].'">';
                                $table .= '<input type="hidden" name="JUL REAL" value="'.$r['JUL_REAL'].'">';
                                $table .= '<input type="hidden" name="AGO PREV" value="'.$r['AGO_PREV'].'">';
                                $table .= '<input type="hidden" name="AGO REAL" value="'.$r['AGO_REAL'].'">';
                                $table .= '<input type="hidden" name="SET PREV" value="'.$r['SET_PREV'].'">';
                                $table .= '<input type="hidden" name="SET REAL" value="'.$r['SET_REAL'].'">';
                                $table .= '<input type="hidden" name="OUT PREV" value="'.$r['OUT_PREV'].'">';
                                $table .= '<input type="hidden" name="OUT REAL" value="'.$r['OUT_REAL'].'">';
                                $table .= '<input type="hidden" name="NOV PREV" value="'.$r['NOV_PREV'].'">';
                                $table .= '<input type="hidden" name="NOV REAL" value="'.$r['NOV_REAL'].'">';
                                $table .= '<input type="hidden" name="DEZ PREV" value="'.$r['DEZ_PREV'].'">';
                                $table .= '<input type="hidden" name="DEZ REAL" value="'.$r['DEZ_REAL'].'">';
                                $table .= '<button class="btn btn-primary"><i class="fa fa-calendar-o" aria-hidden="true"></i></i> Editar </i></button>'; //
                                $table .= '</form></td>';



                                //MODAL COM OS GRÁFICOS
                                $table .= '<td><form action="graf-qualite.php" method="post">'; 
                                $table .= '<input type="hidden" name="ID" value="'.$r['ID'].'">';
                                $table .= '<input type="hidden" name="KPI" value="'.$r['KPI'].'">';
                                $table .= '<input type="hidden" name="PILOTE" value="'.$r['PILOTE'].'">';
                                $table .= '<input type="hidden" name="JAN PREV" value="'.$r['JAN PREV'].'">';
                                $table .= '<input type="hidden" name="JAN REAL" value="'.$r['JAN REAL'].'">';
                                $table .= '<input type="hidden" name="FEV PREV" value="'.$r['FEV PREV'].'">';
                                $table .= '<input type="hidden" name="FEV REAL" value="'.$r['FEV REAL'].'">';
                                $table .= '<input type="hidden" name="MAR PREV" value="'.$r['MAR PREV'].'">';
                                $table .= '<input type="hidden" name="MAR REAL" value="'.$r['MAR REAL'].'">';
                                $table .= '<input type="hidden" name="ABR PREV" value="'.$r['ABR PREV'].'">';
                                $table .= '<input type="hidden" name="ABR REAL" value="'.$r['ABR REAL'].'">';
                                $table .= '<input type="hidden" name="MAI PREV" value="'.$r['MAI PREV'].'">';
                                $table .= '<input type="hidden" name="MAI REAL" value="'.$r['MAI REAL'].'">';
                                $table .= '<input type="hidden" name="JUN PREV" value="'.$r['JUN PREV'].'">';
                                $table .= '<input type="hidden" name="JUN REAL" value="'.$r['JUN REAL'].'">';
                                $table .= '<input type="hidden" name="JUL PREV" value="'.$r['JUL PREV'].'">';
                                $table .= '<input type="hidden" name="JUL REAL" value="'.$r['JUL REAL'].'">';
                                $table .= '<input type="hidden" name="AGO PREV" value="'.$r['AGO PREV'].'">';
                                $table .= '<input type="hidden" name="AGO REAL" value="'.$r['AGO REAL'].'">';
                                $table .= '<input type="hidden" name="SET PREV" value="'.$r['SET PREV'].'">';
                                $table .= '<input type="hidden" name="SET REAL" value="'.$r['SET REAL'].'">';
                                $table .= '<input type="hidden" name="OUT PREV" value="'.$r['OUT PREV'].'">';
                                $table .= '<input type="hidden" name="OUT REAL" value="'.$r['OUT REAL'].'">';
                                $table .= '<input type="hidden" name="NOV PREV" value="'.$r['NOV PREV'].'">';
                                $table .= '<input type="hidden" name="NOV REAL" value="'.$r['NOV REAL'].'">';
                                $table .= '<input type="hidden" name="DEZ PREV" value="'.$r['DEZ PREV'].'">';
                                $table .= '<input type="hidden" name="DEZ REAL" value="'.$r['DEZ REAL'].'">';
                                $table .= '<!-- Button -->
                                            <button type="button" class="btn btn-info button" data-toggle="modal" data-target="#exampleModal" name="button" onclick="abreModal(' . $r['ID'] .');">
                                              Gráfico
                                            </button>

                                            <!-- Modal -->

                                            <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" onload="click("button");">
                                              <div class="modal-dialog" role="document">
                                                <div class="modal-content">
                                                  <div class="modal-header">
                                                    <h5 class="modal-title" id="exampleModalLabel">Gráfico</h5>
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                      <span aria-hidden="true">&times;</span>
                                                    </button>
                                                    </div>
                                                    <div class="modal-body">
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                                    </div>
                                                </div>
                                              </div>
                                            </div>'; //
                                $table .= '</form></td>';

                            }

                            //AQUI É ONDE DEVERIA FICAR A SOMA 
                            $table .= '<tr>
                                          <td></td>
                                          <td>Total</td>
                                          <td></td>
                                          <td>Soma Jan_Prev</td>
                                          <td>Soma Jan_Real</td>
                                          <td>Soma Fev_Prev</td>
                                          <td>Soma Fev_Real</td>
                                          <td>Soma Mar_Prev</td>
                                          <td>Soma Mar_Real</td>
                                          <td>Soma Abr_Prev</td>
                                          <td>Soma Abr_Real</td>
                                          <td>Soma Mai_Prev</td>
                                          <td>Soma Mai_Real</td>
                                          <td>Soma Jun_Prev</td>
                                          <td>Soma Jun_Real</td>
                                          <td>Soma Jul_Prev</td>
                                          <td>Soma Jul_Real</td>
                                          <td>Soma Ago_Prev</td>
                                          <td>Soma Ago_Real</td>
                                          <td>Soma Set_Prev</td>
                                          <td>Soma Set_Real</td>
                                          <td>Soma Out_Prev</td>
                                          <td>Soma Out_Real</td>
                                          <td>Soma Nov_Prev</td>
                                          <td>Soma Nov_Real</td>
                                          <td>Soma Dez_Prev</td>
                                          <td>Soma Dez_Real</td>
                                      </tr>';

                            //Finalizando a tabela
                            $table .= '</tbody></table>';


                            //Imprimindo a tabela
                            echo '<div class="table-table" style="overflow-x:auto;">';
                            echo $table;
                    ?>

I did the middle line manually there to exemplify, but I have no idea how to add the values. Someone can help me?

TEST 1: I tried to make the sum within the ifs which print the table, but gave a result that sure is not right. I put in the code above the changes.

inserir a descrição da imagem aqui

  • 1

    Mariana, you can do the sum using the while of the table body, using a variable to accumulate the totals

2 answers

2

User a variable inside the while loop to store the values, remember to use the += operator to not lose the last values, at the end write this variable in the fields you want

  • So it went bad. I’ll edit with the result.

0


So the problem was very simple.

Doing this I was concatenating and not adding.

$jan_prev .=+ $r['JAN_PREV'];

I switched to that and it worked.

$jan_prev =+ $r['JAN_PREV'];

Browser other questions tagged

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