我应该如何从同一变量中的先前值中减去?

时间:2013-04-05 15:19:03

标签: php

问题

我遇到的问题是无法动态减去expected_weight的损失。我希望在playing_dates计数完成之前更改expected_weight。请参考预期的输出。提前谢谢!

代码:

echo "<pre>";
        $benchmark = 96.5; //The first weight from which a person commits to lose their weight every week.
        $lose = 10; //Percentage of how much they commit to lose
        $lose_weight = ($benchmark/100)*$lose; //This gives how much they should lose every week to meet the target

        while($row_teams = mysql_fetch_assoc($result_select_teams)){
            echo"   {<br />";
            echo"       benchmark ==> ".$benchmark." Kg<br />";
            echo"       loss% ==> ".$lose."%<br />";
            echo"       lose_weight ==> ".$lose_weight." Kg<br /><br />";
            echo"       my_teams ==> ".$row_teams['t_name']."<br /><br />";
            $playing_dates = $row_teams['playing_dates'];
            $playing_dates = explode('|', $playing_dates);

            $date = date('Y');
            $noofweighins = count($playing_dates);
            echo"       noofweighins ==> ".$noofweighins."<br /><br />";
            $lose_atleast = round($lose_weight/$noofweighins, 2);
            $expected_weight = 0;
                for($k=0; $k<count($playing_dates);$k++){
                    $expected_weight = $benchmark - $lose_atleast;
                    if(substr($playing_dates[$k], -4)<=$date){
                        echo"       my_playing_dates ==> ".$playing_dates[$k]." to lose ==> ".$lose_atleast."<br />";
                        echo"       Expected weight ==> ".$expected_weight."<br /><br />";
                    }
                }
            echo"   }<br />";
        }
    }
    echo "</pre>";

输出

benchmark ==> 96.5 Kg
        loss% ==> 10%
        lose_weight ==> 9.65 Kg

        noofweighins ==> 10

        my_playing_dates ==> 16/02/2013 to lose ==> 0.965
        Expected weight ==> 95.535

        my_playing_dates ==> 18/02/2013 to lose ==> 0.965
        Expected weight ==> 95.535

        my_playing_dates ==> 13/03/2013 to lose ==> 0.965
        Expected weight ==> 95.535

        my_playing_dates ==> 20/03/2013 to lose ==> 0.965
        Expected weight ==> 95.535

        my_playing_dates ==> 27/03/2013 to lose ==> 0.965
        Expected weight ==> 95.535

        my_playing_dates ==> 03/04/2013 to lose ==> 0.965
        Expected weight ==> 95.535

        my_playing_dates ==> 10/04/2013 to lose ==> 0.965
        Expected weight ==> 95.535

        my_playing_dates ==> 17/04/2013 to lose ==> 0.965
        Expected weight ==> 95.535

        my_playing_dates ==> 24/04/2013 to lose ==> 0.965
        Expected weight ==> 95.535

        my_playing_dates ==> 01/05/2013 to lose ==> 0.965
        Expected weight ==> 95.535

预期输出

benchmark ==> 96.5 Kg
        loss% ==> 10%
        lose_weight ==> 9.65 Kg

        noofweighins ==> 10

        my_playing_dates ==> 16/02/2013 to lose ==> 0.965
        Expected weight ==> 95.535 (96.5 - 0.965)

        my_playing_dates ==> 18/02/2013 to lose ==> 0.965
        Expected weight ==> 94.57 (95.535 - 0.965)

        my_playing_dates ==> 13/03/2013 to lose ==> 0.965
        Expected weight ==> 93.605 (94.57 - 0.965)

        my_playing_dates ==> 20/03/2013 to lose ==> 0.965
        Expected weight ==> 92.64 (93.605 - 0.965)

        my_playing_dates ==> 27/03/2013 to lose ==> 0.965
        Expected weight ==> 91.675 (92.64 - 0.965)

        my_playing_dates ==> 03/04/2013 to lose ==> 0.965
        Expected weight ==> 90.71 (91.675 - 0.965)

        my_playing_dates ==> 10/04/2013 to lose ==> 0.965
        Expected weight ==> 89.745 (90.71 - 0.965)

        my_playing_dates ==> 17/04/2013 to lose ==> 0.965
        Expected weight ==> 88.78 (89.745 - 0.965)

        my_playing_dates ==> 24/04/2013 to lose ==> 0.965
        Expected weight ==> 87.815 (88.78 - 0.965)

        my_playing_dates ==> 01/05/2013 to lose ==> 0.965
        Expected weight ==> 86.85 (87.815 - 0.965)

2 个答案:

答案 0 :(得分:1)

我真的不知道PHP或其他什么,我只是浏览你的代码,但如果你将$expected_weight = $benchmark - $lose_atleast;更改为$expected_weight = $expected_weight - $lose_atleast;

会发生什么?

答案 1 :(得分:0)

期望的结果是: Expected weight ==> 87.815 (88.78 - 0.965)

虽然 $benchmark = 96.5;始终保持不变。

其公式为: $expected_weight = $benchmark - $lose_atleast;

您似乎必须在循环中更改$benchmark以获得所需的结果。