2016-01-31 60 views
1

我正在嘗試編寫一個函數,用於查找汽車上保險支付的$InterestRate值。我已經插入的代碼見下表:我如何高效地在php中編寫查找表?

目前我曾嘗試:

function interestRate() { 
if ((int)$VehicleYear >= 2001) && (int)$VehicleYear <= 2005){ 

    $Term = 24; 

    if ((int)$CreditScore >= 720){ 
     $InterestRate = 15.90; 
    } 
    elseif ((int)$CreditScore >= 610) || ((int)$CreditScore <= 719) { 
     $InterestRate = 19.90; 
    } 
    elseif ((int)$CreditScore >= 580) || ((int)$CreditScore <= 609) { 
     $InterestRate = 22.90; 
    } 
    elseif ((int)$CreditScore >= 530) || ((int)$CreditScore <= 579) { 
     $InterestRate = 22.90; 
    } 
    elseif ((int)$CreditScore >= 489) || ((int)$CreditScore <= 529) { 
     $InterestRate = 22.90; 
    } 
} 

if ((int)$VehicleYear == 2006){ 

    $Term = 24; 

    if ((int)$CreditScore >= 720){ 
     $InterestRate = 15.90; 
    } 
    elseif ((int)$CreditScore >= 610) || ((int)$CreditScore <= 719) { 
     $InterestRate = 18.90; 
    } 
    elseif ((int)$CreditScore >= 580) || ((int)$CreditScore <= 609) { 
     $InterestRate = 21.90; 
    } 
    elseif ((int)$CreditScore >= 530) || ((int)$CreditScore <= 579) { 
     $InterestRate = 22.90; 
    } 
    elseif ((int)$CreditScore >= 489) || ((int)$CreditScore <= 529) { 
     $InterestRate = 22.90; 
    } 
} 
if ((int)$VehicleYear == 2007){ 

    $Term = 36; 

    if ((int)$CreditScore >= 720){ 
     $InterestRate = 15.90; 
    } 
    elseif ((int)$CreditScore >= 610) || ((int)$CreditScore <= 719) { 
     $InterestRate = 18.90; 
    } 
    elseif ((int)$CreditScore >= 580) || ((int)$CreditScore <= 609) { 
     $InterestRate = 21.90; 
    } 
    elseif ((int)$CreditScore >= 530) || ((int)$CreditScore <= 579) { 
     $InterestRate = 22.90; 
    } 
    elseif ((int)$CreditScore >= 489) || ((int)$CreditScore <= 529) { 
     $InterestRate = 22.90; 
    } 
} 
if ((int)$VehicleYear == 2008){ 

    $Term = 36; 

    if ((int)$CreditScore >= 720){ 
     $InterestRate = 14.90; 
    } 
    elseif ((int)$CreditScore >= 610) || ((int)$CreditScore <= 719) { 
     $InterestRate = 17.90; 
    } 
    elseif ((int)$CreditScore >= 580) || ((int)$CreditScore <= 609) { 
     $InterestRate = 21.90; 
    } 
    elseif ((int)$CreditScore >= 530) || ((int)$CreditScore <= 579) { 
     $InterestRate = 21.90; 
    } 
    elseif ((int)$CreditScore >= 489) || ((int)$CreditScore <= 529) { 
     $InterestRate = 21.90; 
    } 
} 

if ((int)$VehicleYear == 2009){ 

    $Term = 36; 

    if ((int)$CreditScore >= 720){ 
     $InterestRate = 14.90; 
    } 
    elseif ((int)$CreditScore >= 610) || ((int)$CreditScore <= 719) { 
     $InterestRate = 17.90; 
    } 
    elseif ((int)$CreditScore >= 580) || ((int)$CreditScore <= 609) { 
     $InterestRate = 20.90; 
    } 
    elseif ((int)$CreditScore >= 530) || ((int)$CreditScore <= 579) { 
     $InterestRate = 20.90; 
    } 
    elseif ((int)$CreditScore >= 489) || ((int)$CreditScore <= 529) { 
     $InterestRate = 20.90; 
    } 
} 

if ((int)$VehicleYear == 2010){ 

    $Term = 36; 

    if ((int)$CreditScore >= 720){ 
     $InterestRate = 14.90; 
    } 
    elseif ((int)$CreditScore >= 610) || ((int)$CreditScore <= 719) { 
     $InterestRate = 17.90; 
    } 
    elseif ((int)$CreditScore >= 580) || ((int)$CreditScore <= 609) { 
     $InterestRate = 20.90; 
    } 
    elseif ((int)$CreditScore >= 530) || ((int)$CreditScore <= 579) { 
     $InterestRate = 20.90; 
    } 
    elseif ((int)$CreditScore >= 489) || ((int)$CreditScore <= 529) { 
     $InterestRate = 20.90; 
    } 
} 

if ((int)$VehicleYear == 2011){ 

    $Term = 42; 

    if ((int)$CreditScore >= 720){ 
     $InterestRate = 14.90; 
    } 
    elseif ((int)$CreditScore >= 610) || ((int)$CreditScore <= 719) { 
     $InterestRate = 16.90; 
    } 
    elseif ((int)$CreditScore >= 580) || ((int)$CreditScore <= 609) { 
     $InterestRate = 19.90; 
    } 
    elseif ((int)$CreditScore >= 530) || ((int)$CreditScore <= 579) { 
     $InterestRate = 19.90; 
    } 
    elseif ((int)$CreditScore >= 489) || ((int)$CreditScore <= 529) { 
     $InterestRate = 20.90; 
    } 
} 

if ((int)$VehicleYear == 2012){ 

    $Term = 42; 

    if ((int)$CreditScore >= 720){ 
     $InterestRate = 12.90; 
    } 
    elseif ((int)$CreditScore >= 610) || ((int)$CreditScore <= 719) { 
     $InterestRate = 15.90; 
    } 
    elseif ((int)$CreditScore >= 580) || ((int)$CreditScore <= 609) { 
     $InterestRate = 18.90; 
    } 
    elseif ((int)$CreditScore >= 530) || ((int)$CreditScore <= 579) { 
     $InterestRate = 19.90; 
    } 
    elseif ((int)$CreditScore >= 489) || ((int)$CreditScore <= 529) { 
     $InterestRate = 19.90; 
    } 
} 

if ((int)$VehicleYear == 2013){ 

    $Term = 42; 

    if ((int)$CreditScore >= 720){ 
     $InterestRate = 12.90; 
    } 
    elseif ((int)$CreditScore >= 610) || ((int)$CreditScore <= 719) { 
     $InterestRate = 15.90; 
    } 
    elseif ((int)$CreditScore >= 580) || ((int)$CreditScore <= 609) { 
     $InterestRate = 18.90; 
    } 
    elseif ((int)$CreditScore >= 530) || ((int)$CreditScore <= 579) { 
     $InterestRate = 18.90; 
    } 
    elseif ((int)$CreditScore >= 489) || ((int)$CreditScore <= 529) { 
     $InterestRate = 18.90; 
    } 
} 

if ((int)$VehicleYear >= 2014){ 

    $Term = 48; 

    if ((int)$CreditScore >= 720){ 
     $InterestRate = 11.90; 
    } 
    elseif ((int)$CreditScore >= 610) || ((int)$CreditScore <= 719) { 
     $InterestRate = 14.90; 
    } 
    elseif ((int)$CreditScore >= 580) || ((int)$CreditScore <= 609) { 
     $InterestRate = 17.90; 
    } 
    elseif ((int)$CreditScore >= 530) || ((int)$CreditScore <= 579) { 
     $InterestRate = 18.90; 
    } 
    elseif ((int)$CreditScore >= 489) || ((int)$CreditScore <= 529) { 
     $InterestRate = 18.90; 
    } 
} 
} 

它既是乏味的,只是當我嘗試在計算中使用$InterestRate不起作用。解決這個問題的最好方法是什麼?

編輯:有人提到使用SQL表。我不知道該怎麼做。但這裏是我的表基本上

<table style="undefined;table-layout: fixed; width: 497px"> 
 
<colgroup> 
 
<col style="width: 93px"> 
 
<col style="width: 73px"> 
 
<col style="width: 75px"> 
 
<col style="width: 64px"> 
 
<col style="width: 64px"> 
 
<col style="width: 64px"> 
 
<col style="width: 64px"> 
 
</colgroup> 
 
    <tr> 
 
    <th>Vehicle Year</th> 
 
    <th>Term</th> 
 
    <th>FICO</th> 
 
    <th>FICO</th> 
 
    <th>FICO</th> 
 
    <th>FICO</th> 
 
    <th>FICO</th> 
 
    </tr> 
 
    <tr> 
 
    <td></td> 
 
    <td></td> 
 
    <td>720 +</td> 
 
    <td>719-610</td> 
 
    <td>609-580</td> 
 
    <td>579-530</td> 
 
    <td>529-489</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2014-current</td> 
 
    <td>48</td> 
 
    <td>11</td> 
 
    <td>14</td> 
 
    <td>17</td> 
 
    <td>18</td> 
 
    <td>18</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2013</td> 
 
    <td>42</td> 
 
    <td>12</td> 
 
    <td>15</td> 
 
    <td>18</td> 
 
    <td>18</td> 
 
    <td>18</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2012</td> 
 
    <td>42</td> 
 
    <td>12</td> 
 
    <td>15</td> 
 
    <td>18</td> 
 
    <td>19</td> 
 
    <td>19</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2011</td> 
 
    <td>42</td> 
 
    <td>14</td> 
 
    <td>16</td> 
 
    <td>19</td> 
 
    <td>19</td> 
 
    <td>20</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2010</td> 
 
    <td>36</td> 
 
    <td>14</td> 
 
    <td>17</td> 
 
    <td>20</td> 
 
    <td>20</td> 
 
    <td>20</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2009</td> 
 
    <td>36</td> 
 
    <td>14</td> 
 
    <td>17</td> 
 
    <td>20</td> 
 
    <td>20</td> 
 
    <td>20</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2008</td> 
 
    <td>36</td> 
 
    <td>14</td> 
 
    <td>17</td> 
 
    <td>21</td> 
 
    <td>21</td> 
 
    <td>21</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2007</td> 
 
    <td>36</td> 
 
    <td>15</td> 
 
    <td>18</td> 
 
    <td>21</td> 
 
    <td>22</td> 
 
    <td>21</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2006</td> 
 
    <td>24</td> 
 
    <td>15</td> 
 
    <td>18</td> 
 
    <td>21</td> 
 
    <td>22</td> 
 
    <td>22</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2001-2005</td> 
 
    <td>24</td> 
 
    <td>15</td> 
 
    <td>19</td> 
 
    <td>22</td> 
 
    <td>22</td> 
 
    <td>22</td> 
 
    </tr> 
 
</table> 
 
Extra o

+0

無論你傳遞'$ ssn'和'$ VehicleYear'值到這個函數,你也不會使用'global'。你是如何在函數內獲得這個'$ ssn'和'$ VehicleYear'的值的? –

+0

哦對不起,讓我解決這個問題 – Tendi

回答

1

在這裏你去:所有重複的代碼被刪除:

function creditScore($cs) { 
    $cs = (int) $cs; 

    if  ($cs >= 720)    return 5; 
    elseif ($cs >= 610 && $cs <= 719) return 4; 
    elseif ($cs >= 580 && $cs <= 609) return 3; 
    elseif ($cs >= 530 && $cs <= 579) return 2; 
    elseif ($cs >= 489 && $cs <= 529) return 1; 
    else        return 0; 
} 

function interestRate($vehicleYear, $creditScore) { 
    $vehicleYear = (int) $vehicleYear; 
    $cred = creditScore($creditScore); 
    if (! $cred)  // handle credit score below 489: returns 0 
     throw new Exception("Unsupported creditScore: $creditScore"); 
    $cred--; // otherwise it's 1..5, change to 0..4 for array index 

    $rates = [ 
     2001 => [ 'term' => 24, 'rates' => [ 22.90, 22.90, 22.90, 19.90, 15.90 ] ], 
     2002 => [ 'term' => 24, 'rates' => [ 22.90, 22.90, 22.90, 19.90, 15.90 ] ], 
     2003 => [ 'term' => 24, 'rates' => [ 22.90, 22.90, 22.90, 19.90, 15.90 ] ], 
     2004 => [ 'term' => 24, 'rates' => [ 22.90, 22.90, 22.90, 19.90, 15.90 ] ], 
     2005 => [ 'term' => 24, 'rates' => [ 22.90, 22.90, 22.90, 19.90, 15.90 ] ], 
     2006 => [ 'term' => 24, 'rates' => [ 22.90, 22.90, 21.90, 18.90, 15.90 ] ], 
     2007 => [ 'term' => 36, 'rates' => [ 22.90, 22.90, 21.90, 18.90, 15.90 ] ], 
     2008 => [ 'term' => 36, 'rates' => [ 22.90, 22.90, 21.90, 17.90, 14.90 ] ], 
     2009 => [ 'term' => 36, 'rates' => [ 20.90, 20.90, 20.90, 17.90, 14.90 ] ], 
     2010 => [ 'term' => 36, 'rates' => [ 20.90, 20.90, 20.90, 17.90, 14.90 ] ], 
     2011 => [ 'term' => 42, 'rates' => [ 20.90, 19.90, 19.90, 16.90, 14.90 ] ], 
     2012 => [ 'term' => 42, 'rates' => [ 19.90, 19.90, 18.90, 15.90, 12.90 ] ], 
     2013 => [ 'term' => 42, 'rates' => [ 18.90, 18.90, 18.90, 15.90, 12.90 ] ], 
     2014 => [ 'term' => 48, 'rates' => [ 18.90, 18.90, 17.90, 14.90, 11.90 ] ], 
    ]; 

    return [ // TODO: check if isset($rates[$vehicleYear ]) 
     $rates[$vehicleYear]['rates'][ $cred ], 
     $rates[$vehicleYear]['term'] 
    ]; 
} 

list($interestRate, $term) = interestRate(2013, 666); 
+0

我不確定這是如何實際工作的,以及如何使用它。順便提一下,我正在用適當的變量更新問題。 – Tendi

+0

好吧,我會解釋這一點,以及如何在查詢表中不使用SQL。幾分鐘。 – Kenney

+0

我創建了一個表並將其放在問題的詳細信息中,以查看我正在處理的問題 – Tendi