2011-11-01 76 views
0

我在我的網站上運行訂閱。 我有一個1,3,6和12個月的訂閱,我希望用戶能夠隨時更改訂閱。 但是,我需要計算用戶在短期內註冊時必須支付的金額,而不是相對便宜,更長的一筆。計算錯誤,請幫我查找?

我做了這個函數optimized_subscription_total($active_sub_time,$arr_sub_values),以便它完全返回那筆錢。

<?php 


function optimized_subscription_total($active_sub_time,$arr_sub_values) 
{ 

    // This function takes a row from the DB where prices for each period of time usage is listed. there are prices for 1 month, 3 months,6 and 12 months. 

    // when the user has subscribed for 12 months, and the user asks for a refund, after they used 9 months and 6 days for example, the system treats the refund as if they subscribed for (in months) COST_6 + COST_3 + (COST_1/30)*6 
    // the result of the function is then subtracted from the amount they actually paid and is considered the refund. 

    // $arr_sub_values is the associative row from the DB, containing the prices 
    // $active_sub_time is measured in months and is a double 

$result=0; 


while(($active_sub_time-12)>=0) 
{ 
    $active_sub_time-=12; 
    $result+=($arr_subscription_values['COST_12']); 
} 

while(($active_sub_time-6)>=0) 
{ 
    $active_sub_time-=6; 
    $result+=($arr_subscription_values['COST_6']); 
} 
while(($active_sub_time-3)>=0) 
{ 
    $active_sub_time-=3; 
    $result+=($arr_subscription_values['COST_3']); 
} 

while(($active_sub_time-1)>=0) 
{ 
    $active_sub_time-=1; 
    $result+=($arr_subscription_values['COST_1']); 
} 

if($active_sub_time>0) 
    $result+=($active_sub_time)*($arr_subscription_values['COST_1']); 

return $result; 

} 



$datetime1 = date_create('2009-12-11'); 
$datetime2 = date_create('2010-11-09'); 
$interval = date_diff($datetime1, $datetime2); 
$num_of_months = ($interval->format('%y'))*12+($interval->format('%m'))+($interval->format('%a'))/30; 
echo "<br />"; 

$v = array('COST_1'=>'3.99','COST_3'=>'9.99','COST_6'=>'15.99','COST_12'=>'25.99'); 
echo "OPT value for $num_of_months months=" . optimized_subscription_total($num_of_months, $v); 


?> 

奇怪的是,我得到刷新這個代碼後出現只有經過7〜10倍的bug。 所以我得到了:

OPT value for 10 months=M.97 

作爲一個結果在這裏。我想我需要得到一個浮點數,不是嗎?

我在期待函數的結果應該是「OPT值10個月= 29.97」,因爲它應該花費COST_6 + COST_3 + COST_1 ...但是我得到那個奇怪的M.97,有時候像POKHHHG.97

+0

他從什麼變化到什麼?例如,我在12個月的訂閱,我在第二個月,現在我更改訂閱3個月,你在哪裏存儲所有這些值? –

+0

這只是關於該功能用於什麼的一些概述。如果您全年(12個月)支付了25.99美元,並在7個月後進行紓困或升級,則視爲您已經有6個月+ 1個月的費用。並從先前支付的25.99中獲得退款。在這裏你可以看到它是:refund = 25.99-(15.99 + 3.99)= 6.01如果你使用這個函數並運行它,10次後你可以看到輸出是錯誤的。如「10個月的OPT值= M.97 」,其中M.97很奇怪。我試圖找出是什麼原因造成的。 – Ted

+0

我沒有測試代碼,因爲我目前沒有PHP服務器,如果事情仍然困擾你,請告訴我。 –

回答

0

我會改變邏輯到下面,看看是否仍然產生錯誤。我認爲這是一個更清晰,易於調試。這與你的解釋完全相同。

while($active_sub_time>=12) 
{ 
    $active_sub_time-=12; 
    $result+=($arr_subscription_values['COST_12']); 
} 

while($active_sub_time>=6) 
{ 
    $active_sub_time-=6; 
    $result+=($arr_subscription_values['COST_6']); 
} 
while($active_sub_time>=3) 
{ 
    $active_sub_time-=3; 
    $result+=($arr_subscription_values['COST_3']); 
} 

while($active_sub_time>=1) 
{ 
    $active_sub_time-=1; 
    $result+=($arr_subscription_values['COST_1']); 
} 

我還會做的是在頂部內部函數中添加調試鱈魚。

echo "<br>$active_sub_time" // debug code include at the top 

這會給你一個想法,如果有任何垃圾被髮送到函數本身。

此外,如果上述問題沒有解決問題,我會在所有塊中添加此測試功能。

if (!is_numeric($result)) 
{ 
    echo"<br> Bug occurred";break; // print other values if necessary 
} 
+0

謝謝@鱷魚獵人,我剛剛添加了is_numeric函數尾巴。這太奇怪了,我把這個值打印出來(現在是I.00425),但服務器正常處理它,它只是打印出來。我得到的gettype是雙倍的。該函數是否也爲您返回錯誤的值? – Ted