How to make the result of a column?

Asked

Viewed 46 times

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 and the following: inserir a descrição da imagem aqui

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

1 answer

2


You can add up inside the while

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

Then you use the variable $valorTotal in the appropriate place

Like 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>

Browser other questions tagged

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