2013-11-28 57 views
0

我有一个长的IFer语句来检查URL并显示一个按钮,Im努力简化代码,这样它也将更容易维护。我宁愿循环通过所有的页面。它我怎样才能简化我的代码,以便于维护

if(($currentUrl == 'http://example.com/example/management_toolkit.php')){ 
     echo '<div class="prevbutton"><a href="'. HOSTNAME .'index.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentUrl == 'http://example.com/example/management_toolkit.php')){ //TODO : change the URLs managemen toolkit.. 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'index.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentPageButton == 'organising_delegating')){ 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit/organising_delegating.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
} 

elseif(($currentUrl == 'http://example.com/example/management_toolkit/organising_delegating.php')){ //TODO : change the URLs 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentPageButton == 'problem_solving')){ 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit/problem_solving.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; //TODO : change the URLs for Leadership 
} 

elseif(($currentUrl == 'http://example.com/example/management_toolkit/problem_solving.php')){ //TODO : change the URLs 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentPageButton == 'exit_transition_onboard')){ 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit/exit_transition_onboard.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; //TODO : change the URLs for Leadership 
} 

elseif(($currentUrl == 'http://example.com/example/management_toolkit/exit_transition_onboard.php')){ //TODO : change the URLs 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentPageButton == 'recruitment_onboarding')){ 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit/recruitment_onboarding.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; //TODO : change the URLs for Leadership 
} 

elseif(($currentUrl == 'http://example.com/example/management_toolkit/recruitment_onboarding.php')){ //TODO : change the URLs 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentPageButton == 'manage')){ 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit/manage.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; //TODO : change the URLs for Leadership 
} 

elseif(($currentUrl == 'http://example.com/example/management_toolkit/manage.php')){ //TODO : change the URLs 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentPageButton == 'leadership')){ 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit/leadership.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; //TODO : change the URLs for Leadership 
} 

elseif(($currentUrl == 'http://example.com/example/management_toolkit/leadership.php')){ //TODO : change the URLs 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentPageButton == 'grow_develop_coach')){ 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit/grow_develop_coach.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; //TODO : change the URLs for Leadership 
} 

elseif(($currentUrl == 'http://example.com/example/management_toolkit/grow_develop_coach.php')){ //TODO : change the URLs 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentPageButton == 'reward_recognition')){ 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit/reward_recognition.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; //TODO : change the URLs for Leadership 
} 

elseif(($currentUrl == 'http://example.com/example/management_toolkit/reward_recognition.php')){ //TODO : change the URLs 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentPageButton == 'planning')){ 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit/planning.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; //TODO : change the URLs for Leadership 
} 

elseif(($currentUrl == 'http://example.com/example/management_toolkit/planning.php')){ //TODO : change the URLs 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
}elseif(($currentPageButton == 'communication')){ 
    echo '<div class="prevbutton"><a href="'. HOSTNAME .'management_toolkit/communication.php" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; //TODO : change the URLs for Leadership 
} 

回答

-1

使用switch语句:

switch($currentUrl) {  
case 'http://example.com/example/management_toolkit/planning.php': 
    $url = 'management_toolkit/planning.php'; 
    break; 
case 'http://example.com/example/management_toolkit/reward_recognition.php': 
    $url = 'management_toolkit/reward_recognition.php'; 
    break; 
default : 
    $url = 'home.php'; 
    break; 
} 
echo '<div class="prevbutton"><a href="'. HOSTNAME . $url . '" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 
1

你的代码基本上重复自己...

$irRelevantURL = "http://example.com/example/"; 
$length = stripos($currentUrl,irRelevantURL); 
      +strlen(irRelevantURL); 
$relevant = substr($currentUrl,$length); 
echo '<div class="prevbutton"><a href="'. HOSTNAME .$relevant'" class="previousNavbutton"><span style="padding-left:5px;">Back</span></a></div>'; 

而且,你可能意味着$HOSTNAME ...