How to make the result of a column?

0

if(($resultado_usuario) AND ($resultado_usuario->num_rows != 0)){
	?>
		<style>
	
	tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #CCC}
	    
	</style>
	

	
	<table class="table table-striped table-bordered table-hover">
		<thead>
			<tr>
			
			<th><h4><span style="color: black"><strong>ID</th>
			<th><h4><span style="color: black"><strong>Tipo de Venda</th>
			<th><h4><span style="color: black"><strong>Tipo de Pagamento</th>
			<th><h4><span style="color: black"><strong>Total</th>

			</tr>
		</thead>
		<tbody>
			<?php
			while($row_usuario = mysqli_fetch_assoc($resultado_usuario)){
				?>
				<tr>
				    
					<td><?php echo $row_usuario['id']; ?></th>
					<td><?php echo $row_usuario['tipo_venda']; ?></th>
					<td><?php echo $row_usuario['tipo']; ?></th>
					<td><?php echo $row_usuario['total']; ?></th>

					
					
				

				</tr>
				<?php
			}?>
		</tbody>
	</table>
<?php

Have this table that is formed after a query in the database where the result is the following:

My question in the Total field how do I add all the results ??

    
asked by anonymous 13.12.2018 / 02:45

1 answer

2

You can go by summing within while

   <?php
    $valorTotal = 0;    
    while($row_usuario = mysqli_fetch_assoc($resultado_usuario)){
            $valorTotal += $row_usuario['total'];
    ?>

Then you use the $valorTotal variable in the appropriate place

Type this:

 <tbody>
         <?php
         $valorTotal = 0;   
         while($row_usuario = mysqli_fetch_assoc($resultado_usuario)){
            $valorTotal += $row_usuario['total'];
         ?>
            <tr>

                <td><?php echo $row_usuario['id']; ?></th>
                <td><?php echo $row_usuario['tipo_venda']; ?></th>
                <td><?php echo $row_usuario['tipo']; ?></th>
                <td><?php echo $row_usuario['total']; ?></th>


            </tr>
            <?php
        }?>
        <tr>
           <td>Total:</th>
           <td><?php echo $valorTotal; ?></th>
        </tr>
    </tbody>
    
13.12.2018 / 02:57