2013-04-05 56 views
1

問題我應該如何從相同變量中的前一個值中減去?

我現在面臨的問題是我不能夠減去expected_weight動態的喪失。我希望expected_weight可以改變,直到play_dates計數完成。請參閱預期的輸出。提前致謝!

代碼:

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) 

回答

1

我真的不知道PHPØ [R任何東西,我只能走馬觀花看了你的代碼,但如果你改變$expected_weight = $benchmark - $lose_atleast;$expected_weight = $expected_weight - $lose_atleast;

+0

嘿凱爾有一點改動,工程!非常感謝 – colourtheweb 2013-04-05 15:36:07

+0

沒問題,很高興我能幫上忙。請你能+1這篇文章,我想建立一些代表:) – James 2013-04-05 15:42:20

0

期望的結果是發生了什麼: Expected weight ==> 87.815 (88.78 - 0.965)

雖然 $benchmark = 96.5;保持不變,所有的時間。

而且公式是: $expected_weight = $benchmark - $lose_atleast;

看來你必須改變你$benchmark在循環中得到期望的結果。

+0

我想顯示每個播放日期的預期重量,所以期望的結果是不是預期**重量==> 87.815(88.78 - 0.965)* *。期望的輸出中提到了所需的結果。 – colourtheweb 2013-04-05 15:31:44

+0

你剛剛寫了'87.815(88.78 - 0.965)'。因此,'87.815 = 88.78 - 0.965'必須匹配'$ expected_weight = $ benchmark - $ lose_atleast;'在期望的結果中,您從不同的東西中減去,而不僅僅是'96.5'。但是你的'基準測試'仍然是一樣的。 – vikingmaster 2013-04-05 15:34:59

相關問題