我有一个很长的IF if语句检查网址并显示一个按钮,我正在努力简化代码,以便它也更容易维护。我更喜欢循环遍历所有tohse页面。
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
}
答案 0 :(得分: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
......
答案 1 :(得分:-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>';