Problem with PHP Expense Calculation

Asked

Viewed 74 times

0

I’m trying to calculate expenses in the system but I’m not succeeding. The system works as follows, the User makes an order involving 1 or more products, these orders are stored in a table. Each Product registered in the system is stored in another table, having the standard fields, in stock, unitary cost, already sent and pending. The User has two tables, one for the Branch he manages and the other is the users account.

The problem is, having order value for each branch. That is, add up how many items of the product were sent to the affiliate and then multiply by the cost of it, and finally, add up the entire result to display the total expense of the affiliate at the time.

I tried using while however it exhibited several times the same product in different quantities to the same affiliate.

SUBTITLES

subsidiaries = table where all registered subsidiaries are;

stationary items = table where the products are;

stationary requests = table where the orders are;

requested file = table where the products requested in the order are;

qnt_sent = Quantity Sent;

cost = Unitary cost per product;

chns = Unique code for each data registered in the system, works as a protocol number.

PHP I am using PDO for connection

$procura_filial = $site->query("SELECT * FROM filiais ORDER BY codigo ASC");
while ($filiais = $procura_filial->fetch(PDO::FETCH_OBJ)) {
    $procura_solicitacoes = $site->query("SELECT * FROM papelaria_solicitacoes WHERE filial = '$filiais->chns'");
    if ($procura_solicitacoes->rowCount() <= 0) {
    }else {
        while($solicitacoes = $procura_solicitacoes->fetch(PDO::FETCH_OBJ)){
            $procura_solicitados = $site->query("SELECT * FROM papelaria_solicitados WHERE chns = '$solicitacoes->chns'");
            while ($solicitados = $procura_solicitados->fetch(PDO::FETCH_OBJ)) {
                $reprocura_solicitados = $site->query("SELECT * FROM papelaria_solicitados WHERE filial = '$solicitados->filial' and produtos = '$solicitados->produtos'");
                //SOMA QUANTIDADE ENVIADA DE CADA PRODUTO
                while($soma_qnt = $reprocura_solicitados->fetch(PDO::FETCH_OBJ)){
                    $val1 = $soma_qnt->qnt_enviada;
                    $total_cont += $val1;
                    $total_produto_env = $total_cont;
                }
            }
        }
    }
    echo "Filial: ".$soma_qnt->filial." Produto: ".$soma_qnt->produtos." Enviados: ".$total_produto_env."<br>";
}

1 answer

0

I see that you are going a lot on the requested chart or is it wrong. You should treat these calculations directly in the database without having to make several Selects and several whiles. Your program will be very slow like this.

Do something like:

SELECT filial,count(*) as total FROM papelaria_solicitados GROUP BY filial,produtos

And then display only on a while.

I hope I helped, Greetings,

  • Rodrigo, could you explain to me better your suggestion, I did not understand right.

  • Which part you didn’t understand right?

Browser other questions tagged

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