0

quiero hacer una tabla de amortización donde el usuario dará los datos como:

  • Capital
  • Interes
  • Periodo

La fórmula es multiplicar interés por capital y sumarse al capital las veces que marca el periodo, por ejemplo si capital es 100 interés 0.5 y periodo 10 tendría que ser la operación

100x0.5=50 y ese resultado se suma a capital 50+100=150 y eso se repetiría 10 veces. Se que se podría usar un for pero tengo dudas de cómo hacerlo.

1
  • 1
    Comparte el código de lo que has hecho, es importante que incluyas lo que tu has hecho y el error que tienes a la hora de ejecutar tu código.
    – allexiusw
    el 29 jun. 2020 a las 10:19

2 respuestas 2

1

Prueba lo siguiente:

<?php

if(isset($_POST["interes"]))

{

    $_POST["interes"]=str_replace(",",".",$_POST["interes"]);

}

?>

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8">

    <title>Tabla Amortización/title>

</head>

 

<style>

form {width:250px;}

form>div>span {width:100px;display: inline-block;text-align:left;}

form input {width:150px;}

form>div {text-align:center;}

</style>

 

<body>

 

<h1>Tabla de Amortización con PHP</h1>

<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST">

    <div>

        <span>Importe :</span>

        <span><input type="text" name="importe" maxlength=9 value="<?php echo $_POST["importe"]?>"></span>

    </div>

    <div>

        <span>Años :</span>

        <span><input type="text" name="anos" maxlength=2 value="<?php echo $_POST["anos"]?>"></span>

    </div>

    <div>

        <span>Interés :</span>

        <span><input type="text" name="interes" maxlength=9 value="<?php echo $_POST["interes"]?>"></span>

    </div>

    <div>

        <p><input type="submit" value="Calcular"></p>

    </div>

</form>

 

<?php

if($_POST["importe"] && $_POST["anos"] && $_POST["interes"])

{

    $deuda=$_POST["importe"];

    $anos=$_POST["anos"];

    $interes=$_POST["interes"];

 

    // hacemos los calculos...

    $interes=($interes/100)/12;

    $m=($deuda*$interes*(pow((1+$interes),($anos*12))))/((pow((1+$interes),($anos*12)))-1);

 

    echo "<div>Capital Inicial: ".number_format($deuda,2,",",".")." €";

    echo "<br>Cuota a pagar mensualmente: ".number_format($m,2,",",".")." €</div>";

    ?>

    <table border=1 cellpadding=5 cellspacing=0>

        <tr>

            <th>Mes</th>

            <th>Intereses</th>

            <th>Amortización</th>

            <th>Capital Pendiente</th>

        </tr>

        <?php

        // mostramos todos los meses...

        for($i=1;$i<=$anos*12;$i++)

        {

            echo "<tr>";

                echo "<td align=right>".$i."</td>";

                $totalint=$totalint+($deuda*$interes);

                echo "<td align=right>".number_format($deuda*$interes,2,",",".")."</td>";

                echo "<td align=right>".number_format($m-($deuda*$interes),2,",",".")."</td>";

 

                $deuda=$deuda-($m-($deuda*$interes));

                if ($deuda<0)

                {

                    echo "<td align=right>0</td>";

                }else{

                    echo "<td align=right>".number_format($deuda,2,",",".")."</td>";

                }

            echo "</tr>";

        }

        ?>

    </table>

    Pago total de intereses : <?php echo number_format($totalint,2,",",".")?> €

    <?php

}

?>

 
</body>

</html>
0

Creo que quieres algo como esto:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet"href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <title>Document</title>
</head>
<body>

<div class="form col-md-8 mx-auto">
    <div class="card">
        <div class="card-header">asd</div>
        <div class="card-body">
            <form action="" method="POST">
                <label for="capital">Capital </label>
                <input type="number" step="0.01" name="capital" id="capital" class="form-control" required>

                <label for="interes">Interes </label>
                <input type="number" step="0.01" name="interes" id="interes" class="form-control" required>

                <label for="periodo">Periodo </label>
                <input type="number" name="periodo" id="periodo" class="form-control" required><br>
                <input type="submit" id="generate" name="generate" value="Generar tabla" class="btn btn-success form-control">
            </form>
        </div>
        
    </div>
    <?php 
    

    if(isset($_POST['generate'])){
        $capital = $_POST['capital'];
        $interes = $_POST['interes'];
        $periodo = $_POST['periodo'];
        ?>
        <table class="table">
            <thead>
                <th>Periodo</th>
                <th>Capital</th>
                <th>Interes</th>
                
            </thead>
            <tbody>
                <?php
                    $i = 0;
                    
                    while($periodo > $i){
                        $t = $capital*$interes;
                        $capital += $t;
                    echo $t;
                        ?>
                        <tr>
                            <td><?php echo($i+1); ?></td>
                            <td><?php echo($capital); ?></td>
                            <td><?php echo($interes); ?></td>
                            
                        </tr>
                        <?php
                        $i++;

                    }

                 ?>

            </tbody>
        </table>


        <?php
    }
    ?>
</div>

No entiendo bien como la quieres, según yo debería llevar más campos, pero esa es la idea, si quieres más ayuda sube un ejemplo de como quedaría la tabla y si puedes ya llena.

1
  • genial muchas gracias con esto arregle mi problema.
    – John
    el 4 jul. 2020 a las 6:00

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.