2014-10-02 212 views
2

我試圖找到更好的方式來編寫這個邏輯。使用PHP類FDF,我檢查from的複選框的值,並將圖像添加到PDF中的座標。重構如果/其他語句

 if ($salutation[0] == "Dr.") { 

      $pdf->Image('/inc/checked.png',31.4, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',43.5, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',56, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',70.5, 105.5,-300); 

     }elseif ($salutation[0] == "Mr.") { 

      $pdf->Image('/inc/checked.png',43.5, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',31.4, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',56, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',70.5, 105.5,-300); 

     }elseif ($salutation[0] == "Mrs.") { 

      $pdf->Image('/inc/checked.png',56, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',31.4, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',43.5, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',70.5, 105.5,-300); 

     }elseif ($salutation[0] == "Ms.") { 

      $pdf->Image('/inc/checked.png',70.5, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',31.4, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',43.5, 105.5,-300); 
      $pdf->Image('/inc/unchecked.png',56, 105.5,-300); 
     } 

switch語句看起來好像差不多一樣。對我來說問題是,無論如何,其他圖像仍然需要添加。有一個更好的方法嗎?

+2

'switch'語句適合這個麪糊 – 2014-10-02 15:27:58

+1

爲什麼不使用循環? – 2014-10-02 15:29:21

回答

2

所有的最簡單的方法是如何使用三元的運營商。不要通過使用花哨的數組,函數或循環來簡化這個簡單的問題。所以你可以用4行代碼替換整個東西:

$pdf->Image('/inc/' . (($salutation[0] == 'Dr.') ? '' : 'un') . 'checked.png',31.4, 105.5,-300); 
$pdf->Image('/inc/' . (($salutation[0] == 'Mr.') ? '' : 'un') . 'checked.png',43.5, 105.5,-300); 
$pdf->Image('/inc/' . (($salutation[0] == 'Mrs.') ? '' : 'un') . 'checked.png',56, 105.5,-300); 
$pdf->Image('/inc/' . (($salutation[0] == 'Ms.') ? '' : 'un') . 'checked.png',70.5, 105.5,-300); 

不客氣。 :)

+0

同意。我喜歡一個很好的循環,但很難打敗它。 – 2014-10-02 16:00:42

+0

確實。我也喜歡好的圈,但我更喜歡三元運營商。 :) – 2014-10-02 16:11:08

0

嘗試開關

switch($salutation[0]){ 
    case 'Dr.': 
     //do stuff 
    break; 

    case 'Mr.': 
     //do stuff 
    break; 

    case 'Mrs.': 
     //do stuff 
    break; 
} 
+0

我仍然需要每個案例添加4個圖像。 – 2014-10-02 15:36:54

2

$coords = array(
    'Dr.' => array(31.4, 105.5,-300), 
    'Mr.' => array(43.5, 105.5,-300), 
    'Mrs.' => array(56, 105.5,-300), 
    'Ms.' => array(70.5, 105.5,-300) 
); 

foreach ($coords as $title => $coord) { 
    if($salutation[0] == $title) { 
     $pdf->Image('/inc/checked.png', $coord[0], $coord[1], $coord[2]); 
    } else { 
     $pdf->Image('/inc/unchecked.png', $coord[0], $coord[1], $coord[2]); 
    } 
} 
+0

數組中的第一行缺少逗號。 – 2014-10-02 16:29:01

+0

@WonderBred好點 - 我已經糾正它。 – Jez 2014-10-03 10:18:06

0

您可以簡化您的圖像的創造和使用一個簡單的函數,它的4個數字減少反覆代碼:

switch($salutation[0]){ 
    case 'Dr.': 
     handleImages(31.4, 43.5, 56, 70.5); 
     break; 
    case 'Mr.': 
     handleImages(43.5, 31.4, 56, 70.5); 
     break; 
    case 'Mrs.': 
     handleImages(56, 43.5, 31.4, 70.5); 
     break; 
    case 'Ms.': 
     handleImages(70.5, 56, 43.5, 31.4); 
     break; 
} 

function handleImages($a, $b, $c, $d) { 
    $pdf->Image('/inc/checked.png', $a, 105.5, -300); 
    $pdf->Image('/inc/unchecked.png', $b, 105.5, -300); 
    $pdf->Image('/inc/unchecked.png', $c, 105.5, -300); 
    $pdf->Image('/inc/unchecked.png', $d, 105.5, -300); 
} 

然後,您可以簡化交換機中調用函數

0

由於這是PHP - 它會使我們的工作變得非常簡單!你可以使用關聯數組。

的確有做以下的一種更好的方式 - 但我喜歡下面這段代碼的簡潔性:

$checkedPos = array ("Dr." => 31.4, "Mr." => 43.5, "Mrs." => 56, "Ms." => 70.5); 
$unchecked1Pos = array ("Dr." => 43.5, "Mr." => 56, "Mrs." => 70.5, "Ms." => 31.4); 
$unchecked2Pos = array ("Dr." => 56, "Mr." => 70.5, "Mrs." => 31.4, "Ms." => 43.5); 
$unchecked3Pos = array ("Dr." => 70.5, "Mr." => 31.4, "Mrs." => 43.5, "Ms." => 56); 

$pdf->Image('/inc/checked.png', $checkedPos[$salutation[0]], 105.5, -300); 
$pdf->Image('/inc/unchecked.png', $unchecked1Pos[$salutation[0]], 105.5, -300); 
$pdf->Image('/inc/unchecked.png', $unchecked2Pos[$salutation[0]], 105.5, -300); 
$pdf->Image('/inc/unchecked.png', $unchecked3Pos[$salutation[0]], 105.5, -300); 

希望有所幫助。