0

在此代码片段中,所有值都将由数据库驱动,我需要更好地替换此代码。

佣金百分比将根据任务价格范围计算。

$commission1=10;//in percentage
     $commission2=20;
     $commission3=30;
     $taskprice =200;
     if($taskprice >=0 && $taskprice <=150)
       {
    $adminearnings = ($taskprice *$commission1 ) / 100 ;
    $Geniepayment = $taskprice - $adminearnings;
       }
    else if($taskprice >=151 && $taskprice <=300)
      {
     $adminearnings = ($taskprice * $commission2) / 100 ;
     $Geniepayment = $taskprice - $adminearnings;
      }
   else if($taskprice >=301 && $taskprice <=450)
     {
    $adminearnings = ($taskprice * $commission3 ) / 100 ;
    $Geniepayment = $taskprice - $adminearnings;
     }
 echo $taskprice.'</br>';
 echo $adminearnings.'</br>';
 echo $Geniepayment.'</br>';
4

3 回答 3

0

至于我,我会做一个简单的功能来快速访问你需要的东西:

function calculate($commission, $taskprice)
{
    $arrayCommission = array(0, 10, 20, 30);
    $adminearnings = ($taskprice * $arrayCommission[$commission] ) / 100 ;
    $Geniepayment = $taskprice - $adminearnings;
    return array($taskprice, $adminearnings, $Geniepayment);
}

那将是这样的:

if($taskprice >=0 && $taskprice <=150)
{
   $arrayAnswer = calculate(1, $taskprice);
}
elseif($taskprice >=151 && $taskprice <=300)
{
   $arrayAnswer = calculate(2, $taskprice);
}
elseif($taskprice >=301 && $taskprice <=450)
{
   $arrayAnswer = calculate(3, $taskprice);
}
于 2013-03-07T04:21:51.400 回答
0

我同意其他人的观点,即条件是必需的。我会重构以提取计算,如下所示:

<?php

// Returns array(earnings, payment)
//
function getEarningsPayment($taskprice, $commission)
{
    $earnings = ($taskprice * $commission) / 100 ;
    $payment = $taskprice - $adminearnings;
    return array ($earnings, $payment);
}


$commission1=10;//in percentage
$commission2=20;
$commission3=30;
$taskprice =200;

if($taskprice >=0 && $taskprice <=150)
    $commission = $commission1;
else if($taskprice >=151 && $taskprice <=300)
    $commission = $commission2;
else if($taskprice >=301 && $taskprice <=450)
    $commission = $commission3;

list ($adminearnings, $Geniepayment) = getEarningsPayment($taskprice, $commission);

echo $taskprice.'</br>';
echo $adminearnings.'</br>';
echo $Geniepayment.'</br>';

?>
于 2013-03-07T04:36:28.417 回答
0

伙计们,这个想法怎么样..等待您的反馈和建议

$commissions = array
  (
  '0'=>array
  (
  "commission_start_range"=>"0","commission_end_range"=>"100","commission_amount"=>"10"
  ),
  '1'=>array
  (
  "commission_start_range"=>"101","commission_end_range"=>"200","commission_amount"=>"20"
  ),
  '2'=>array
  (
  "commission_start_range"=>"201","commission_end_range"=>"300","commission_amount"=>"30"
  )
  );

function check_commission($task_price,$commissions)
    {
    foreach($commissions as $commission_data)
    {
    $start = $commission_data['commission_start_range'];
    $end   = $commission_data['commission_end_range'];
    $amount= $commission_data['commission_amount'];
    if($task_price >=$start && $task_price <=$end)
    {
    $admin_earnings = ($task_price * $amount) / 100 ;
    $genie_payment = $task_price - $admin_earnings;
    return  $admin_earnings;//echo $admin_earnings;
    }
    }
    }
    check_commission(200,$commissions);
于 2013-03-08T08:40:45.590 回答