显示for循环的特定记录

时间:2014-04-21 09:41:48

标签: php for-loop

我有一个for循环,显示22到60岁之间的数据集

$amount = 3150;
$age = 22;
$fv_21 = futureValue('.045', 1, 0, $amount) * -1;
echo $fv_21;
echo "<br>";

$fee_21 = round($fv_21 / 100 * 1.5, 2);
echo $fee_21;
echo "<br>";

$balance = $fv_21 - $fee_21;
echo $balance;
echo "<br><br><br>";
//exit;

for ($x = $age; $x <= 60; $x++) {
    //calculate super amount
    $amount = round($amount * 1.025, 2);
    echo "amount_$x = $amount<br>";

    //calculate FV
    $fv = round(futureValue('.045', 1, $amount, $balance) * -1,2);
    echo "FV of age $x is : " . $fv;
    echo "<br>";

    //calculate fee
    $fee = round(futureValue('.045', 1, $amount, $balance) * -1 / 100 * 1.5, 2);
    echo "Fee of age $x is: $fee";
    echo "<br>";
    //calculate balance
    $balance = $fv - $fee;
    echo "Balance of age $x is : ".round($balance,2);
    echo "<br><br><br>";
}

这是我得到的输出

amount_22 = 3228.75
FV of age 22 is : 6617.03
Fee of age 22 is: 99.26
Balance of age 22 is : 6517.77


amount_23 = 3309.47
FV of age 23 is : 10120.54
Fee of age 23 is: 151.81
Balance of age 23 is : 9968.73


amount_24 = 3392.21
FV of age 24 is : 13809.53
Fee of age 24 is: 207.14
Balance of age 24 is : 13602.39

我想实现仅显示特定年龄的数据,例如,如果用户登录系统,如果他的年龄是23岁,那么仅显示23岁的数据,我该如何从循环中提取这些数据?

重要提示:23岁的数据取决于22岁的数据,24岁的数据取决于23岁的数据,依此类推。

如果有人能指出我正确的方向,我将非常感激。

更新 这是我正在使用的futureValue()函数

function futureValue($rate, $nper, $pmt, $pv = 0, $type = 0)
{
    if ($nper < 0) {
        return PEAR::raiseError('Number of periods must be positive');
    }
    if ($type != FINANCE_PAY_END && $type != FINANCE_PAY_BEGIN) {
        return PEAR::raiseError('Payment type must be FINANCE_PAY_END or FINANCE_PAY_BEGIN');
    }

    if ($rate) {
        $fv = -$pv * pow(1 + $rate, $nper) - $pmt * (1 + $rate * $type) * (pow(1 + $rate, $nper) - 1) / $rate;
    } else {
        $fv = -$pv - $pmt * $nper;
    }
    return $fv;
}

2 个答案:

答案 0 :(得分:1)

将登录用户的年龄存储到像这样的会话变量中

session_start()
$_SESSION['age']="23";// some value here

并且在for循环中添加if this like

for ($x = $age; $x <= 60; $x++) {
    $amount = round($amount * 1.025, 2);
    $fv = round(futureValue('.045', 1, $amount, $balance) * -1,2);
    $fee = round(futureValue('.045', 1, $amount, $balance) * -1 / 100 * 1.5, 2);
    $balance = $fv - $fee;

    if($age == $_SESSION['age']) {  <- new condition added

    echo "amount_$x = $amount<br>";
    echo "FV of age $x is : " . $fv;
    echo "<br>";

    echo "Fee of age $x is: $fee";
    echo "<br>";

    echo "Balance of age $x is : ".round($balance,2);
    echo "<br><br><br>";
    }
}

答案 1 :(得分:0)

试试这个,

for ($x = $age; $x <= 60; $x++) {

    //calculate super amount
    $amount = round($amount * 1.025, 2);

    //calculate FV
    $fv = round(futureValue('.045', 1, $amount, $balance) * -1,2);

    //calculate fee
    $fee = round(futureValue('.045', 1, $amount, $balance) * -1 / 100 * 1.5, 2);

    //calculate balance
    $balance = $fv - $fee;

    if($x == $loggedInUserId) {
        echo "amount_$x = $amount<br>";
        echo "FV of age $x is : " . $fv;
        echo "<br>";
        echo "Fee of age $x is: $fee";
        echo "<br>";
        echo "Balance of age $x is : ".round($balance,2);
        echo "<br><br><br>";
    }

}