How to add server elements

Asked

Viewed 62 times

1

Hi, I’d like to know how to add up the values that pass me from the server. I created a system that controls expenses/accounts, you register one of your accounts.

EX:

Name: Car
Value: R$ 10,000 (per month)
Times: 5
Total: $value * times. (R$ 50,000)


Name: House
Value: R$ 20,000 (per month)
Times: 5
Total: $value * times. (R$ 100,000)


GENERAL TOTAL ...(HOW TO DO ?)

Only now I want to add the total of the first 'account' to the total of the second 'account'. How do I do this ?
Code:

session_start();
 include("includes/conexa.php");
require_once 'init.php';
require 'check.php';
$voce = $_SESSION['user_name'];
$id = $_SESSION['user_id'];
include("includes/topotw.php"); 
echo "$voce e Seu ID: $id";

echo "";
include('includes/conexa.php');
$consulta = $PDO->query("SELECT * FROM  `contas`  WHERE id_pessoa LIKE '%".$id."%'");  
    while($linha = $consulta ->fetch(PDO::FETCH_ASSOC)) {  
      header('Content-Type: text/html; charset=utf-8');
     

Conta:

{$linha['nome_conta']}

Valor:

R$ {$linha['valor_conta']}

Vezes a Pagar:

{$linha['vezes_conta']}
$vl = $linha['valor_conta']; $vx = $linha['vezes_conta']; $total = $vl * $vx;

Total:

$total
}
?>
  • Andre Junior, show the code

  • Okay, I’ll edit it out.

1 answer

1

Create a variable outside the loop and then inside the loop add up with each total, and display outside the loop:

session_start();
 include("includes/conexa.php");
require_once 'init.php';
require 'check.php';
$voce = $_SESSION['user_name'];
$id = $_SESSION['user_id'];

$totalGeral = 0;
include("includes/topotw.php"); 
echo "$voce e Seu ID: $id";

echo "";
include('includes/conexa.php');
$consulta = $PDO->query("SELECT * FROM  `contas`  WHERE id_pessoa LIKE '%".$id."%'");  
    while($linha = $consulta ->fetch(PDO::FETCH_ASSOC)) {  
      header('Content-Type: text/html; charset=utf-8');

Conta: 

{$linha['nome_conta']}


Valor: 

R$ {$linha['valor_conta']}


Vezes a Pagar:

{$linha['vezes_conta']}

      $vl = $linha['valor_conta'];
            $vx = $linha['vezes_conta'];
            $total = $vl * $vx;

Total: 

 {$total} 

$totalGeral += $total;

    } 


 TOTAL GERAL: <?php echo $totalGeral;?>
?>
  • It is "working" only that the first error appears "Notice: Undefined variable: totalGeral in /home/u603863053/public_html/system_account/accounts.php on line 26 "

  • Strange.. You could post your code on Pastebin.com ?

  • https://pastebin.com/V3CRJ1zR

Browser other questions tagged

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