I calculate in foreach php with jquery

Asked

Viewed 161 times

1

would like to know if it is possible to run a real time calculation with jquery inside a php foreach and how? this is my code:

<?php
            $Read = new Read;
            $Read->ExeRead("c100web", "WHERE codclient = :client", "client={$Clie}");
            if (!$Read->getResult()):
                VARErro("Não á registros na lista, cadastre os produtos!", INFOR);
            else:
                foreach ($Read->getResult() as $Pedidos):
                    extract($Pedidos);
                    $Style = "";
                    $ReadProd = new Read;
                    $ReadProd->ExeRead("c007prod", "WHERE codigo = :cod", "cod={$codpro}");
                    if ($ReadProd->getResult()):
                        $Nome = $ReadProd->getResult()[0]['descricao'];
                        $PrecoUni = $ReadProd->getResult()[0]['preco_venda'];
                    endif;
                    ?>
                    <tr> 
                        <td><?= $Nome; ?></td>
                        <td><input disabled=""  type="text" name="vlruni" id="vlruni" value="<?= $PrecoUni; ?>"/></td>
                        <td><input type="number"  name="qnt[]" id="qnt" value="<?= $qntpro; ?>"/></td>
                        <td><input disabled="" type="text" name="vlrtl" id="vlrtl"/></td>
                    </tr>
                    <tr><td colspan="4" style="padding: 4px; background-color: #ccc;"></td></tr>

                    <?php
                endforeach;
                ?>
                    <tr>
                     <td colspan="3">Valor total do pedido:</td>
                     <td><input disabled="" type="text" name="vlrlist" id="vlrlist"/></td>
                    </tr>
            <?php
            endif;
            ?>

is basically like this: vlruni * qnt = vlrtl and vlrtl + vlrtl + vlrtl.... = vlrlist, is it possible?... thank you!

HTML:

<tr> 
                        <td>ACUCAR MASCAVO FAVINHO 500G</td>
                        <td><input disabled=""  type="text" name="vlruni" id="vlruni" value="4.6"/></td>
                        <td><input type="number"  name="qnt[]" id="qnt" value="12"/></td>
                        <td><input disabled="" type="text" name="vlrtl" id="vlrtl"/></td>
                    </tr>
                    <tr><td colspan="4" style="padding: 4px; background-color: #ccc;"></td></tr>

                                            <tr> 
                        <td>ACUCAR MASCAVO FAVINHO 1K</td>
                        <td><input disabled=""  type="text" name="vlruni" id="vlruni" value="7.7"/></td>
                        <td><input type="number"  name="qnt[]" id="qnt" value="10"/></td>
                        <td><input disabled="" type="text" name="vlrtl" id="vlrtl"/></td>
                    </tr>
                    <tr><td colspan="4" style="padding: 4px; background-color: #ccc;"></td></tr>

                                            <tr> 
                        <td>AMENDOIM AMENDUPÃ C/ CASCA 280G</td>
                        <td><input disabled=""  type="text" name="vlruni" id="vlruni" value="2.7"/></td>
                        <td><input type="number"  name="qnt[]" id="qnt" value="5"/></td>
                        <td><input disabled="" type="text" name="vlrtl" id="vlrtl"/></td>
                    </tr>
                    <tr><td colspan="4" style="padding: 4px; background-color: #ccc;"></td></tr>
  • What do you mean by real-time calculation? It has how to best describe?

  • i type a new quantity in the qnt field and at the end of typing the multiplication is performed at the exact moment showing in the vlrtl field, and the sum of vlrtl fields is performed showing in the vlrlist field, it can be given a enter or not, if possible without the enter!

  • I believe that the solution is easier only with Javascript. What is the HTML generated by PHP in this code?

  • Jefferson, can exemplify this code in jsfiddle only with html generated in PHP?

  • edited the question by inserting html!

1 answer

0


In this case you have to use Javascript if you use Jquery:

Call the function when typing, the class you put in the quantity field:

$( ".class_qtd" ).blur(function() {
 SomaTotalProduto();
});

To work this function, in the input id you must put the id + 0 index: Example:

<tr class="linha_produto">
 <td><input type="text" class="class_qtd"id="campo_quantidade_produto1"></td>
 <td><input type="text" id="campo_valor_produto1"></td>
</tr>
<tr class="linha_produto">
 <td><input type="text"  class="class_qtd" id="campo_quantidade_produto2"></td>
 <td><input type="text" id="campo_valor_produto2"></td>
</tr>

function SomaTotalProduto(){   
var nlinha =  $(".linha_produto").length; // coloca essa classe no <tr>

for (var i = 1; i <= nlinha; i++) {    
 valor    = $('#campo_valor_produto' + i).val();    
 qtdeprod = $('#campo_quantidade_produto'+ i).val();

 // VALOR TOTAIS PRODUTO
 total = '0';
 total = (valor * qtdeprod);
 $('#campo_valor_total' + i).val('').val(total);
} 

Browser other questions tagged

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