How to calculate column total and show it in table

1

Hello, I need your help ... I need to take the total for each column and show it below the data table.  As well as this image ..

<?php

if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    $valueToSearch2 = $_POST['valueToSearch2'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT * FROM facturacion where item_mes  LIKE '$valueToSearch' AND item_anio LIKE '$valueToSearch2' ORDER BY 'item_dia' ASC";
    $search_result = filterTable($query);
}
 else {
    $query = "SELECT * FROM 'facturacion'";
    $search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
    $connect = mysqli_connect("localhost", "root", "", "registrador");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}

?>

<!DOCTYPE html>
<html>
    <head>
        <title>PHP HTML TABLE DATA SEARCH</title>
        <style>
            table,tr,th,td
            {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        
        <form action="sss.php" method="post">
            <input type="text" name="valueToSearch" placeholder="Ingrese Mes"><br><br>
            <input type="text" name="valueToSearch2" placeholder="Ingrese Año"><br><br>
            <input type="submit" name="search" value="Filter"><br><br>
            
            <table>
                <tr>
                   
                    <th>Sujetas</th>
                    <th>Agravadas</th>               
                    <th>Total</th>
                    
                </tr>

      <!-- populate table from mysql database -->
                <?php 
                while($row = mysqli_fetch_array($search_result)):?>
                <tr>
                    
                    <td><?php echo $row['item_sujetas'];?></td>
                    <td><?php echo $row['item_gravadas'];?></td>
                    <td><?php echo $row['item_total'];?></td>
                </tr>

                <?php endwhile;?>
            </table>
        </form>
        
    </body>
</html>
    
asked by outsider 05.09.2018 в 09:30
source

3 answers

1

You can use something like this

<?php 
$T=0 //ESTA ES LA VARIABLE QUE SUMARA LA COLUMNA
while($row = mysqli_fetch_array($search_result)):
    $T+=$valor; //AQUI SUMAMOS EL VALOR DE CADA RENGLON
    ?>
    <!--
    AQUI MUESTRAS LA VARIABLE DE CADA RENGLON
    -->
    <tr>
        <td><?=$valor?></td>
    </tr>
<?php endwhile;?>
<!--
ESTO ES EL PIE DE LA TABLA
AQUI MUESTRAS LA VARIABLE AL PIE DE LA TABLA
-->
<tr>
    <td><?=$T?></td>
</tr>
    
answered by 05.09.2018 / 09:48
source
0

You could try using the following expression:

SELECT SUM(columna1, columna2, columna3,...)
FROM tabla
WHERE condición;

It would be better for you to create an internal column that is called total and fill in the example:

INSERT INTO tabla (total)
VALUES ((SELECT SUM(columna1, columna2, columna3,...)
        FROM tabla
        WHERE condición;)); 

EDITING

$queryTotal = "SELECT item_mes, item_anio , SUM(valor) as 'total'  FROM facturacion where item_mes  LIKE '$valueToSearch' AND item_anio LIKE '$valueToSearch2' ORDER BY 'item_dia' ASC";
$total = filterTable($queryTotal);

while($row = mysqli_fetch_array($search_result)):?>
     <tr>
         <td><?php echo $row['total'];?></td>
     </tr>

something like that would be I hope it helps you.

    
answered by 05.09.2018 в 09:49
0

I would make a query where you already totalized or add some pseudo columns to the existing query with the totals:

$query = "SELECT (SELECT SUM(enero) FROM facturacion) total_enero, (SELECT SUM(febrero) FROM facturacion) total_febrero, (SELECT SUM(marzo) FROM facturacion) total_marzo FROM facturacion

    
answered by 05.09.2018 в 15:06