2012-03-03 38 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 
}