2013-11-28 73 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 ...