Increase in the value of records every 3 fields

0

I am presented the problem that when doing the INSERT I must increase the value of the amount every 3 fields, because they are quotas and every 3 installments there must be an increase of 20% of the previous amount, as you can see in the image the increase does it well but it starts me from the field cost 3 and it must be from the field cost 4, I will show the function in which I perform the increase and the insertion of the data, as you can see, I have the variable $ increases = 3, which is what generates me the increase every 3 fields , but as you can see in the table when you start, increase from cost 3, and in the next ones if you do it every 3. And try to equalize the variable to 4 and if you increase from cost 4, but when you do the other increase does it every 4 fields, it would have to be in this way cost 1: 20000, cost2: 20000, cost 3: 20000, cost 4: 24400, cost 5: 24400, cost 6: 24400, cost 7: 28800, cost 8: 28800, cost 9: 345 60, cost 10: 34560

function cuotas($cantidad,$monto){

$query = "INSERT INTO cuotas (cantidad) VALUES('$cantidad')";
$sql = $mysqli->query($query);

$val = mysqli_insert_id($mysqli);

$campos = "";
$valores = "";
$porcentaje = 0.2;
$aumenta = 3;

for($i = 1; $i <= $cantidad; $i++){
  if(($i>0) && ($i % $aumenta)==0):
    $monto = $monto*(1+$porcentaje);
  endif;
  $campos .= 'costo' . $i . ' , ';
  $valores .= '' . $monto . ' , ';
}

if($campos != ''):
  $campos = substr($campos, 0, -2);
  $valores = substr($valores, 0, -2);
endif;

$query2 = "INSERT INTO costo_cuotas(id_cantidad,$campos)VALUES($val,$valores)";
$sql .= implode(",", $valores).")";
$sql2 = $mysqli->query($query2);

if($sql>0 && $sql2>0):
  header('Location:admin.php');
else:
  echo mysqli_error($mysqli);
endif;
}
    
asked by 12.08.2017 в 15:58
source

1 answer

0

Good morning

The image that accompanies your approach does not show the expected effect or at least the one achieved so far by code; On the basis that it only shows the structure of the table to be filled out, I will comment on it below .

create in php an array that through iteration you fill with the values to insert in each field; example:

$totalValores=12; // Numero de campos que contiene tu tabla
$valorInsertar = 20000;  // De la imagen corresponde al campo costo1 con id_cantidad=1
$porcentajeIncremento=0.20; // Porcentaje que se incrementa
$aumentaCadaPagos=3; // Cada cuantos pagos aumentas; en tu planteamiento lo aumentas cada 3
$valores= array();

for($i=0; $i<$totalValores;$i++){
   if (($i>0) && (($i % $aumentaCadaPagos)==0))
      $valorInsertar=$valorInsertar*(1+$porcentajeIncremento);

   $valores[]=$valorInsertar;
}
// al terminar la iteracción anterior tendrás los valores 
// Ahora armar el SQL INSERT

$cSQL="INSERT INTO CUOTAS VALUES("; //  Asumo la tabla de la ilustación se llama "cuotas".

$cSQL.=implode(",", $valores).")";

// y lo pasas a tu proceso donde se envia la petición a ejecutarse a la BD.

Be useful or give you an idea for your need.

    
answered by 12.08.2017 в 17:01