Perform mathematical operations in real time or array results

0

First of all, a cordial greeting! I tell you my drawback: I have 2 lists of inventory type (one is the origin of the data and the other should print the values entered in the first to be sent by email) the thing is that the values such as VAT, without VAT, profit, carry some mathematical operations whose results should be printed in the Worksheet 2 as I introduce them in the < em> Worksheet 1 now, each worksheet has 70 rows of 9 fields each then in the Worksheet 1 I extract the current values of the same from the BD in an array and I show them in editable inputs in which the user edits their value and these are printed in the Worksheet 2 using the new function > OUTPUT of HTML5 *** (This so that the impressions of the values in the Sheet 2 are shown in real time) ...

The question is whether I can carry out mathematical operations on those values while I am passing them from Form 1 * to Form 2 . , instead of showing the value entered by the user in the Worksheet 1 , it should be shown is the result of the operation (Division, subtraction, multiplication) in the Worksheet 2 (in real time, it should be noted that there are 2 different forms in 2 spreadsheets that are displayed one over the other on the same page because I know how to do this when the input is of the same form and they are together, in addition to being purely HTML5 and in my case I need to use PHP) , I have searched in several parts and can not find anything, the effect that I intend to obtain is like that of the Excel formulas, it would be Great if you could help me, any other information you need can be requested. Annex relevant fragments of the code:

    <?php

    require ('php/conexion.php');

    /* REALIZO CONSULTA PARA LA TABLA 1 Y LOS ALMACENO EN UN ARRAY */

    $consulta ="SELECT * FROM 'planilla' ORDER BY 'planilla'.'cod' ASC LIMIT 70";
    $result= mysql_query($consulta);

    $a = 0;
    while($row = mysql_fetch_array($result)) {
    if ($a<=70)
    {
    $a++; /* ESTA VARIABLE LA HE CREADO PARA QUE CADA DATO EXTRAÍDO DE LA CONSULTA POSEA UN "ID" DIFERENTE (EJM: valor1, valor2, valor3...) */
    ?>
    <tr>
            <td><?php echo $row['cod'];?></td>
            <td><?php echo $row['producto'];?></td>

    <!-- EN ESTA LÍNEA APLICO LA FUNCIÓN DE HTML5 PARA ENVIAR LOS DATOS EN TIEMPO REAL A LA PLANILLA 2 -->
      <td>
        <form id="form_costo<?php echo $a?>" name="form_costo<?php echo $a?>" method="POST" oninput="<?php echo "re_costos" . $a?>.value=parseInt(<?php echo "costos" . $a?>.value)">
          <input type="number" id="<?php echo "costos" . $a?>" name="<?php echo "costos" . $a?>" value="<?php echo $row['costo'];?>" class="form-control">
        </form>
      </td>

    </tr>
    </tbody>
    <?php
    }
    }
    ?>
    </table>
    </div>
    <!--- FIN de la TABLA 1 --->

Now I show you how they are captured in table 2, which is where I need the mathematical operations to be executed in real time. Or if there is a way to apply it to the results of the query to the DB directly to the results extracted by the array, it would also serve me, that is, it would be ruled out of "In real time", I explain: I get results through the array and to these I am applying the mathematical operations (If it is possible, of course) any of the 2 options can be of help. Thanks in advance

<!-- INICIO TABLA 2 --->
<?php   
require ('php/conexion.php');

/* AQUI REALIZO OTRA CONSULTA SÓLO PARA EXTRAER EL "CÓDIGO" Y LOS "PRODUCTOS" Y MOSTRARLOS EN UN ARRAY EN LAS PRIMERAS 2 COLUMNAS */

$consulta2 ="SELECT * FROM 'planilla' ORDER BY 'planilla'.'cod' ASC LIMIT 70";
$result2 = mysql_query($consulta2);

$b = 0;
while($row = mysql_fetch_array($result2)) {
if ($b<=70)
{
$b++; /* AQUI HE CREADO UNA VARIABLE PARA LA MISMA FUNCIONALIDAD DE LOS "ID" EN LA ANTERIOR */

$codigo = $row['cod']; /* ALMACENO CODIGO EN UNA VARIABLE */
$produc = $row['producto']; /* ALMACENO CODIGO EN UNA VARIABLE */

    echo "<TD>$codigo</TD>"; /* IMPRIMO CÓDIGO */
    echo "<TD>$produc</TD>"; /* IMPRIMO PRODUCTO */

/*  CON ESTA PORCIÓN DE CÓDIGO VOY IMPRIMIENDO LOS VALORES DE COSTO QUE EL USUARIO VAYA INTRODUCIENDO EN LA TABLA 1: <output form='form_costo$b' name='re_costos$b' for='costos$b'></output> */
/* EN LA SIGUIENTE LINEA PUEDEN VER COMO SE IMPRIMEN LOS VALORES DE COSTOS QUE SE VAN INTRODUCIENDO EN LA PLANILLA 1 Y ES DONDE NECESITO QUE EN LUGAR DE IMPRIMIR EL VALOR DIRECTAMENTE SE IMPRIMA EL RESULTADO DE LA OPERACION MATEMATICA */
    echo "<TD><output form='form_costo$b' name='re_costos$b' for='costos$b'></output></TD>";
?>
</tbody>
<?php
}
}
?>
<!-- FIN DE TABLA 2 --->

The friend's response helped me to print the result of "No VAT", since I get the PVP value and divide it by 1.21 directly. Now in this case the question would be: How can I assign the values obtained from a previous operation to a variable to combine it with another operation? Example:

RRP - Result of "Without VAT"

    
asked by Anto J 27.12.2017 в 08:00
source

1 answer

1

If you would like to have two tables for example, and that the modification of data in the first one would alter the values in the second one (for example, adding an 18% tax) it is fine as you are doing:

<table border="1">
    <tr>
      <td>producto</td>
      <td>codigo</td>
      <td>valor neto</td>
    </tr>
    <tr>
      <td>pan</td>
      <td>5</td>
      <td>
        <form id="form_costo5" oninput="result5.value=parseInt(costo5.value)*1.18">
          <input type="number" name="costo5" value="0" />
        </form>
      </td>
    </tr>
    <tr>
      <td>queso</td>
      <td>4</td>
      <td>
        <form id="form_costo4" oninput="result4.value=parseInt(costo4.value)*1.18">
          <input type="number" name="costo4" value="0" />
        </form>
      </td>
    </tr>
  </table>

<hr>

  <table border="1">
    <tr>
      <td>producto</td>
      <td>codigo</td>
      <td>total con IVA</td>
    </tr>
    <tr>
      <td>pan</td>
      <td>5</td>
      <td>
        <output form='form_costo5' name='result5' for='costo5'></output>
      </td>
    </tr>
    <tr>
      <td>queso</td>
      <td>4</td>
      <td>
        <output form='form_costo4' name='result4' for='costo4'></output>
      </td>
    </tr>
  </table>

Now, I do not understand why you would want to do that, if you already know the value of $row['costo'] you can print another cell that contains, for example 1.18* $row['costo'] ;

But, assuming that some values are fixed from the database (for example the price) and others are not defined, it would make sense for the user to complete those that are empty.

EDIT: here I show you an example where the income of the amount modifies the value of two outputs: the total c / VAT and the net.

input {
width:auto;
max-width:121px;
}
output {
border:1px solid #ccc;
width:85px;
display:inline-block;
}
<table border="1">
    <tr>
      <td>producto</td>
      <td>codigo</td>
      <td style="width:125px;">valor neto unitario</td>
      <td style="width:125px;">cantidad</td>
      <td >total c/IVA</td>
      <td >total neto</td>
    </tr>
    <tr>
      <td>pan</td>
      <td>5</td>
      <td colspan="2">
        <form id="form_costo5" oninput="result5.value=Number(parseInt(cantidad5.value,10)*parseInt(costo5.value,10)*1.21).toFixed(3);neto5.value=Number(parseInt(cantidad5.value,10)*parseInt(costo5.value,10)).toFixed(3)">
          <input type="number" name="costo5" value="15" />
          <input type="number" name="cantidad5" value="0" />
        </form>
      </td>
       <td colspan="2">
        <output form='form_costo5' name='result5' onchange="alert(this.value)" >0</output>
        <output form='form_costo5' name='neto5' >0</output>
      </td>    
     </tr>
    <tr>
      <td>queso</td>
      <td>4</td>
     <td colspan="2">
        <form id="form_costo4" oninput="result4.value=Number(parseInt(cantidad4.value,10)*parseInt(costo4.value,10)*1.21).toFixed(3);neto4.value=Number(parseInt(cantidad4.value,10)*parseInt(costo4.value,10)).toFixed(3)">
          <input type="number" name="costo4" value="10" />
          <input type="number" name="cantidad4" value="0" />
        </form>
      </td>
      <td colspan="2">
        <output form='form_costo4' name='result4' >0</output>
        <output form='form_costo4' name='neto4' >0</output>
      </td>
    </tr>
  </table>

Your question remains in the air what you intend to do with this table "updated in real time." Because I guess you want to print it or persist it somewhere.

EDIT 2: Using jQuery simplifies this whole operation. In practice you can use classes to generically define the same behavior for all rows:

jQuery(document).ready(function() {

  jQuery('.fila_calculo').on('change keyup','input',function() {
      var $tr=jQuery(this).closest('tr'),
          costo=$tr.find('.costo').val(),
          cantidad=$tr.find('.cantidad').val();
          $tr.find('.total').val(costo*cantidad*1.21);
          $tr.find('.neto').val(costo*cantidad);
  });
});
input {
width:auto;
max-width:121px;
}
output {
border:1px solid #ccc;
width:85px;
display:inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
    <tr>
      <td>producto</td>
      <td>codigo</td>
      <td style="width:125px;">valor neto unitario</td>
      <td style="width:125px;">cantidad</td>
      <td >total c/IVA</td>
      <td >total neto</td>
    </tr>
    <tr class="fila_calculo">
      <td>pan</td>
      <td>5</td>
      <td colspan="2">
          <input type="number" class="costo" value="15" />
          <input type="number" class="cantidad" value="0" />
        
      </td>
       <td colspan="2">
        <output class="total" >0</output>
        <output class='neto' >0</output>
       
      </td>    
     </tr>
    <tr class="fila_calculo">
      <td>queso</td>
      <td>4</td>
     <td colspan="2">
          <input type="number" class="costo" value="10" />
          <input type="number" class="cantidad" value="0" />
       
      </td>
      <td colspan="2">
        <output class="total" >0</output>
        <output class='neto' >0</output>
       </td>
    </tr>
  </table>
    
answered by 27.12.2017 / 13:18
source