2012-03-03 44 views
0

我一直在研究一段代碼,它可以提取一個公會的名稱,並用它來描述公會在線遊戲中殺死的boss /怪物的信息。這場比賽中有許多怪物,每個人都有三個難度設置。我已經設法讓代碼做我想做的事情,但是它有大量的複製和粘貼,並且只能完成大約五分之一的輸入。我真的不能考慮如何讓這個代碼少一個巨大的膨脹。這是3個難度設置中只有一個怪獸的代碼,因爲你可以看到它只有一個。大概還有另外60個!任何人都可以幫助我理解更好的方法來做到這一點。謝謝!使代碼更有效率,更少的複製和粘貼

$sql = 'SELECT * FROM `phpbb_profile_fields_data`'; 

    $result = $db->sql_query($sql); 

    while ($row = $db->sql_fetchrow($result)) 
    {  
    /////////////////////////////////// START - 8 MAN BONETHRASHER 
    $normal = ''; 
    $hard = ''; 
    $nightmare = ''; 
    /////// START - CHECK NORMAL 
    if ($row['pf_kp_em_no_bonethr'] == '1') 
     { 
      $normal = '&nbsp;<img src="/styles/subsilver2/theme/images/soap/no.png" />'; 
     }  
    else if ($row['pf_kp_em_no_bonethr'] == '2') 
     { 
      $normal = ''; 
     }  
    else if (is_null($row['pf_kp_em_no_bonethr'])) 
     { 
      echo "Boss was set as NULL This should not happen!"; 
     } 
    else 
     { 
      echo "Sosia messed up go hit him in the face."; 
     } 
    /////// END - CHECK NORMAL 
    /////// START - CHECK HARD 
    if ($row['pf_kp_em_ha_bonethr'] == '1') 
     { 
      $hard = '&nbsp;<img src="/styles/subsilver2/theme/images/soap/ha.png" />'; 
     }  
    else if ($row['pf_kp_em_ha_bonethr'] == '2') 
     { 
      $hard = ''; 
     }  
    else if (is_null($row['pf_kp_em_ha_bonethr'])) 
     { 
      echo "Boss was set as NULL This should not happen!"; 
     } 
    else 
     { 
      echo "Sosia messed up go hit him in the face."; 
     } 
    /////// END - CHECK HARD  
    /////// START - CHECK NIGHTMARE 
    if ($row['pf_kp_em_kn_bonethr'] == '1') 
     { 
      $nightmare ='&nbsp;<img src="/styles/subsilver2/theme/images/soap/kn.png" />'; 
     }  
    else if ($row['pf_kp_em_kn_bonethr'] == '2') 
     { 
      $nightmare = ''; 
     }  
    else if (is_null($row['pf_kp_em_kn_bonethr'])) 
     { 
      echo "Boss was set as NULL This should not happen!"; 
     } 
    else 
     { 
      echo "Sosia messed up go hit him in the face."; 
     } 
    /////// END - CHECK NIGHTMARE 

if ($normal == '' && $hard == '' && $nightmare == '') 
     { 

     } 
    else 
     { 
      $template->assign_block_vars('8m_bonethrasher', array(
      'VAR1' => $row['pf_guild_name'], 
      'VAR2' => $normal, 
      'VAR3' => $hard, 
      'VAR4' => $nightmare, 
      )); 
     } 

    } 

    $db->sql_freeresult($result);   
+1

不是100%肯定你的要價,但看看'包括()' – 2012-03-03 00:32:23

+0

嗯,我會基本上不得不創建另外60個這樣的代碼塊,我將要改變的是向另一個老闆提交「pf_kp_em_no_bonethr」這一行,所以我想知道是否會有一種方法可以回收代碼,這將阻止我不得不復制那封鎖了60次 – 2012-03-03 00:41:41

+0

嗯,函數+數組? – Synetech 2012-03-03 00:58:07

回答

1

我還是稍微模糊在你想要做什麼的時候,但我會幫助你一槍。

你可能會離開將創建一個完成所有這一切的類。

例如:

class checks { 

    public function checkBosses($normalBoss, $hardBoss, $nightmareBoss) { 

     $difficulties = array(); 

     $difficulties['normal'] = array('boss' => $normalBoss); 
     $difficulties['hard'] = array('boss' => $hardBoss); 
     $difficulties['nightmare'] = array('boss' => $nightmareBoss); 

     foreach ($this->difficulties as $difficulty -> $boss) { 
      $this->difficulties[$difficulty]['result'] = checkDifficulty($boss['boss'], $difficulty); 
     } 

     $normal = $this->difficulties['normal']['result']; 
     $hard = $this->difficulties['hard']['result']; 
     $nightmare = $this->difficulties['nightmare']['result']; 


     if ($normal == '' && $hard == '' && $nightmare == '') { 
      return null; 
     } else { 
      return array(
       'normal' => $normal, 
       'hard' => $hard, 
       'nightmare' => $nightmare, 
      ); 
     } 
    } 

    protected function checkDifficulty($boss, $difficulty) { 

     if ($difficulty == 'normal') { 
      $image = '&nbsp;<img src="/styles/subsilver2/theme/images/soap/no.png" />'; 
     } else if ($difficulty == 'hard') { 
      $image = '&nbsp;<img src="/styles/subsilver2/theme/images/soap/ha.png" />'; 
     } else if ($difficulty == 'nightmare') { 
      $image = '&nbsp;<img src="/styles/subsilver2/theme/images/soap/kn.png" />'; 
     } 

     if ($boss == '1') { 
      return $image; 
     } else if ($boss == '2') { 
      return ''; 
     } else if (is_null($boss)) { 
      echo "Boss was set as NULL This should not happen!"; 
     } else { 
      echo "Sosia messed up go hit him in the face."; 
     } 
    } 
} 

然後,所有你需要做的就是調用:

$checkResult = checks::checkBosses($row['pf_kp_em_no_bonethr'], $row['pf_kp_em_ha_bonethr'], $row['pf_kp_em_kn_bonethr']); 

if ($checkResult != null) { 
    $template->assign_block_vars('8m_bonethrasher', array(
     'VAR1' => $row['pf_guild_name'], 
     'VAR2' => $normal, 
     'VAR3' => $hard, 
     'VAR4' => $nightmare, 
    )); 
} 
+1

我會建議採取所有這些如果/其他方法,並採取一些方法... ...代碼重用 – Gerep 2012-03-03 01:10:30

+0

我會嘗試,並給它,並返回到線程 – 2012-03-03 01:26:24

+0

更新到Gereps建議 – Shattuck 2012-03-03 01:28:24

0

如果你可以檢索老闆的數組,你可以做他們foreach循環運行的代碼相同的位每個老闆是這樣的:

foreach ($bosses as $boss) { 
    //Full code to be repeated for each boss here 
}