Calculating in foreach php with jquery

1

I 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;
            ?>

It's basically like this: vlruni * qnt = vlrtl and vlrtl + vlrtl + vlrtl .... = vlrlist, is it possible? ... thanks!

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>
    
asked by anonymous 22.06.2017 / 21:36

1 answer

0

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

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

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

To function this function, in the id of the inputs 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);
} 
    
22.06.2017 / 22:34