Adding specific keys obtained in SQL query - PHP

0

I have a sql query that returns all the fields in a table:

$registro = mysqli_query($con, "SELECT * FROM agenda ORDER BY procedimento LIMIT $limit, $nroLotes");

My question is, if it is possible, through this query, to add the values of a certain column, associated with the values of another column, eg:

Among other columns there are "Column: Name" where values are names and "Column: Value" where values are numbers. The idea is to add the values associated with the names.

I guess I should do a foreach within the while, but I'm having difficulty with this approach.

Here is the snippet of the code in question:

<?php
//...
$registro = mysqli_query($con, "SELECT * FROM agenda ORDER BY procedimento     LIMIT $limit, $nroLotes");
$tabela = $tabela.'<table class="table table-striped table-condensed table-  hover">
                    <tr>
                  <th width="300">Nome</th>
                  <th width="200">Procedimentos</th>
                  <th width="150">Valor Cobrado do serviço</th>
                  <th width="150">Valor Total dos serviços</th>
                  <th width="150">Porcentagem do Valor Total dos serviços</th>
                  <th width="150">Data</th>
                    </tr>';


while($linha = mysqli_fetch_array($registro)){
$porcentagem = ($linha['valor'] * 30) / 100;
$tabela = $tabela.'<tr>
                        <td>'.$linha['nome_funcionario'].'</td>
                        <td>'.$linha['procedimento'].'</td>
                        <td>'.number_format($linha['valor'], 2, ',', '.').'</td>
                        <td>'.number_format($porcentagem).'</td>
                        //célula que conterá a soma 
                        //<td>'.number_format($soma).'</td>
                        <td>'.fechaNormal($linha['start']).'</td>';     
}
$tabela = $tabela.'</table>';

$array = array(0 => $tabela,
               1 => $lista);

echo json_encode($array);
?>
    
asked by anonymous 18.07.2016 / 05:09

1 answer

1

I may be wrong, but you can achieve this in SQL itself by using SUM() and GROUP BY .

Try to run the following Query in your table (check the field names if they are correct):

SELECT *, SUM('valor') AS SomaValor FROM agenda GROUP BY 'nome' ORDER BY procedimento

See if the result of SomaValor is what you are looking for.

    
18.07.2016 / 07:24