Untitled

 avatar
unknown
plain_text
2 years ago
3.6 kB
4
Indexable
/* ChatGPT */

function investment_growth_shortcode() {
    ob_start();
    ?>
    <div>
        <form method="post" action="" style="max-width: 400px; margin: 0 auto; color: #fff;">
            <label for="investment-amount" style="display: block; margin-bottom: 8px;">Yearly Investment Amount:</label>
            <input type="text" name="investment-amount" value="100" id="investment-amount" style="width: 100%; padding: 8px; margin-bottom: 16px; background-color: #fff; color: #000;">

            <label for="rate-of-return" style="display: block; margin-bottom: 8px;">Rate of Return (%):</label>
            <input type="text" name="rate-of-return" value="10" id="rate-of-return" style="width: 100%; padding: 8px; margin-bottom: 16px; background-color: #fff; color: #000;">

            <label for="investment-years" style="display: block; margin-bottom: 8px;">Investment Years:</label>
            <input type="text" name="investment-years" value="10" id="investment-years" style="width: 100%; padding: 8px; margin-bottom: 16px; background-color: #fff; color: #000;">

            <input type="submit" value="Calculate" style="display: block; width: 100%; padding: 8px; background-color: #007bff; color: #fff; border: none; cursor: pointer;">
        </form>
    </div>

    <?php

    if (isset($_POST['investment-amount']) && isset($_POST['rate-of-return']) && isset($_POST['investment-years'])) {
        var_dump($_POST); // Debugging line: Display form data
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
        $yearly_investment = floatval($_POST['investment-amount']);
        $rate_of_return = floatval($_POST['rate-of-return']) / 100;
        $years = intval($_POST['investment-years']);

        $portfolio_value = investment_growth($yearly_investment, $rate_of_return, $years);
        echo '-------------';
         var_dump($portfolio_value);
die($years);
        echo '<div style="color: #fff;">';
        for ($year = 1; $year <= $years; $year++) {
            echo 'Year ' . $year . ': $' . number_format($portfolio_value[$year - 1], 2) . '<br>';
        }
        echo '</div>';

        echo '<div style="color: #fff;"> ';
        echo '<canvas id="investment-chart"></canvas>';
        echo '<div id="chart-placeholder"></div>';
        echo '</div>';

        echo '<script>';
        echo 'document.addEventListener("DOMContentLoaded", function() {';
        echo 'var investmentData = ' . json_encode($portfolio_value) . ';';
        echo 'var ctx = document.getElementById("investment-chart").getContext("2d");';
        echo 'var myChart = new Chart(ctx, {';
        echo 'type: "line",';
        echo 'data: {';
        echo 'labels: ' . json_encode(range(1, $years)) . ',';
        echo 'datasets: [{';
        echo 'label: "Investment Growth",';
        echo 'data: investmentData,';
        echo 'backgroundColor: "rgba(255, 255, 255, 0.1)",';
        echo 'borderColor: "#fff",';
        echo 'borderWidth: 1';
        echo '}]';
        echo '},';
        echo 'options: {';
        echo 'scales: {';
        echo 'y: {';
        echo 'beginAtZero: true,';
        echo 'color: "#fff"';
        echo '},';
        echo 'x: {';
        echo 'color: "#fff"';
        echo '}';
        echo '}';
        echo '}';
        echo '});';
        echo '});';
        echo '</script>';
    }

    return ob_get_clean();
}
add_shortcode('investment_growth', 'investment_growth_shortcode');







 
Editor is loading...