2016-05-19 35 views
2

我有一張表格,代表一個旅遊計劃系統的價格計算。作爲php數組的多維表(excelsheet)。還是其他想法?

您可以在這裏下載頁: https://www.dropbox.com/s/ctam8iwym9pumjz/Example.xlsx?dl=0

現在,我想建立像一個PHP函數:

priceCalc($公里,$ numberOfPersons,$ doubleTour)

參數值示例:

  • $ km = 130.0;(double)
  • $ numberOfPersons = 4;(整數)
  • $ doubleTour = true;(布爾)

結果必須與這些值= 218.00€(見excelsheet)

你將如何實現它的最簡單的方法(不包括第三方班或擴展 - 純PHP)?

+0

您已經標記了這個問題,用'phpexcel':你其實看着[PHPExcel(https://github.com/PHPOffice/PHPExcel) ? –

+0

是的,但這應該是最後一個選項。我們希望保持系統苗條,不要那麼多第三方課程。 – djsined

+0

這是你的選擇....你問最簡單的方法來實現它;我指出了最簡單的方法....當然,您可以自己編寫所有計算邏輯,使用普通的PHP –

回答

0

Got it!

function getEcoPrice($distance, $persons, $doubleTour) { 
$distance = ceil($distance); //round up to next integer 

$startPrice = 69; 
$endprice = $startPrice; 

$amountPlusPerson = array(0, 0, 20, 20, 10, 10, 10, 10, 10); //first index is just for setting index = number of persons. value to add, if extra person. 
$amountNextDistance = array(0, 10, 10, 10, 10, 10, 10, 10, 10); //first index is just for setting index = number of distance steps. value to add, if next distance is reached. 
$amountDoubleTour = array(-20, -20, -20, -20, -20, -20, -20, -20, -20); //value to add, if doubleTour is enabled. 

$index = 0; 

switch (true) { 
    case $distance <= 130:    
     $index = 0; 
     break; 
    case $distance <= 160: 
     $index = 1; 
     break; 
    case $distance <= 170: 
     $index = 2;    
     break; 
    case $distance <= 180: 
     $index = 3;    
     break; 
    case $distance <= 190: 
     $index = 4; 
     break; 
    case $distance <= 200: 
     $index = 5; 
     break; 
    case $distance <= 210: 
     $index = 6; 
     break; 
    case $distance <= 220: 
     $index = 7; 
     break; 
    case $distance <= 230: 
     $index = 8; 
     break; 
    case $distance > 230: 
     return 99999; 
     break; 
} 

for($i = 0; $i <= $index; $i++) { 
    $endprice += $amountNextDistance[$i]; 
}; 

for ($i = 0; $i <= $persons; $i++) { 
    $endprice += $amountPlusPerson[$i]; 
} 

if ($doubleTour) { 
    $endprice = $endprice * 2 + $amountDoubleTour[$index]; 
} 

return $endprice; 
} 

函數調用:

echo getEcoPrice(200.1, 2, false);