使代码更高效,减少复制和粘贴

时间:2012-03-03 00:27:35

标签: php mysql

我一直在制作一个代码公会名称的代码,并且随之而来的是老板/怪物的信息,公会已经在网络游戏中杀死了。在这个游戏中有很多怪物,每个都有三个难度设置。我已经设法让代码完成我想要的但是它有大量的复制和粘贴,而且我只做了大约1/5的总量。我真的不敢想如何使这个代码不再是一个巨大的膨胀。这是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);         

2 个答案:

答案 0 :(得分: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 :(得分:0)

如果你可以检索一个老板数组,你可以对它们进行foreach循环,为每个老板运行相同的代码,如下所示:

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