Adding the column of a PHP table

Asked

Viewed 823 times

1

Good evening, you guys, I bring in a table a result of a select of a bank, but in the result:

echo"". $T_TOTAL."";

I’m bringing a variable because I had to do a treatment to get my result.

Doubt: How can I add the result of the variable?

<?php
    echo"<table class='datatable table table-hover table-bordered table-responsiv'>";
    echo"<thead>";
    echo"<tr>";
    echo"<th>CELULAR</th>";
    echo"<th>DESCRIÇÃO</th>";
    echo"<th>TEMPO</th>";
    echo"<th>VALOR</th>";
    echo"</tr>";
    echo"</thead>";
    echo"<tbody>";       
    echo"<tr>";
    while ($row = mysql_fetch_array($query_pesquisa)) {
    $TEMPO=$row['VALOR'];
    $T_TOTAL=$TEMPO / 6 * 0.015 ;
    echo"<td>".$row['CELULAR']."</td>";
    echo"<td>".$row['DESCRICAO']."</td>";
    echo"<td>".$row['MINUTOS']."</td>";
    echo"<td>".$T_TOTAL."</td>";
    echo"</tr>";
      } 
   echo" </tbody>";
   echo" </table>";   

?>

2 answers

1

Introduction to operators in PHP

inserir a descrição da imagem aqui

An operator is used to perform operations between one or more values (or expressions, in programming jargon) and return only one final value. We go now to the operators.

Arithmetic operators in PHP

<?php
// Declarando os valores das variáveis
$a = 4;
$b = 2;

?>
<h2>Adição</h2>
<p>
<?php

echo $a + $b;

?>
</p>
<h2>Subtração</h2>
<p>
<?php

echo $a - $b;

?>
</p>
<h2>Multiplicação</h2>
<p>
<?php

echo $a * $b;

?>
</p>
<h2>Divisão</h2>
<p>
<?php

echo $a / $b;

?>
</p>
<h2>Módulo(resto da divisão)</h2>
<p>
<?php

echo $a % $b;

?>
</p>

1


Start variable with zero value.

Inside the loop, increment the value of each sum.

Just don’t confuse subtotal (total unit) with the total general.

Note that more Row has been created <tr></tr> after loop, to display the general total.

I’m not sure if this is the result you want. I was only guided by the most logical deduction.

$T_TOTAL = 0;
while ($row = mysql_fetch_array($query_pesquisa)) {
;
$subotal =$row['VALOR'] / 0.09; // 6 * 0.015
$T_TOTAL += $subotal;
echo "<td>".$row['CELULAR']."</td>";
echo "<td>".$row['DESCRICAO']."</td>";
echo "<td>".$row['MINUTOS']."</td>";
echo "<td>".$subotal."</td>";
echo "</tr>";
  }
echo "<tr><td colspan="3" style="text-align:right;">total: </td><td>".$T_TOTAL."</td></tr>";

In the subtotal calculation part, I changed the expression 6 * 0.015 for 0.09 because if the expression will always be the same, it is redundant to make PHP always calculate the same value. It is more performative (runs faster) if you put the value already calculated.

  • Good afternoon, but I need that calculation (6 * 0.015), if I let it not go right ?

  • It was just a suggestion because if this expression is fixed, then I suggested to leave the calculated value. Therefore, 6 multiplied by 0.015 results 0.09. But if you want you can leave it as 6 * 0.015. Of course it’s a ridiculous performance gain, but anyway it’s a logic for optimization. The fewer processes to achieve the same result and, efficiently, the better.

  • Um got it, thanks for detailing and it’s exactly what I needed,.

Browser other questions tagged

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