2011-08-10 6 views
2

当社は現在、当社のURLの一部にあるナビゲーションで私たちを助けるために、スイッチケースのURLの設定を使用し、そこにそれを行うための簡単な方法があるが、私は見つけるように見えるcouldntの場合イムわからない1.PHPスイッチケースのURL

<?php if (! isset($_GET['step'])) 
    { 
     include('./step1.php'); 

    } else {  
     $page = $_GET['step']; 
     switch($page) 
     { 
      case '1': 
       include('./step1.php'); 
       break; 
      case '2': 
       include('./step2.php'); 
       break; 
     } 
    } 
    ?> 

このシステムは完璧に機能しますが、xxxxxx.phpを入力すると、唯一の空白のページが表示され、「3」を処理するケースはないので、しかし、私が疑問に思っていたのは.. xxxxx.phpに戻すためにそれらの2つ以外のケースについてそれを伝えるかもしれない底に追加できるPHPコードはありますか?

おかげ

ダニエル

答えて

4

defaultケースを使用してください。

<?php if (! isset($_GET['step'])) 
    { 
     include('./step1.php'); 

    } else {  
     $page = $_GET['step']; 
     switch($page) 
     { 
      case '1': 
       include('./step1.php'); 
       break; 
      case '2': 
       include('./step2.php'); 
       break; 
      default: 
       // Default action 
      break; 
     } 
    } 
?> 

デフォルトの場合は、明示的に指定されていないすべてのケースのために実行されます:それはこのような何かにあなたのスイッチを変更する、です。

2

すべてswitch文では、defaultのケースでは、それ以外のケースは実行されません。何かのように...

switch ($foo) 
{ 
    case 1: 
    break; 
    ... 
    default: 
    header("Location: someOtherUrl"); 
} 

となります。しかし、あなたは、他のより堅牢で拡張性の高いページディスパッチソリューションのために、Googleに取り掛かりたいかもしれません。線に沿って何かとは異なるアプローチについて

1

方法:

<?php 
$currentStep = $_GET['step']; 
$includePage = './step'.$currentStep.'.php'; # Assuming the pages are structured the same, i.e. stepN where N is a number 

if(!file_exists($includePage) || !isset($currentStep)){ # If file doesn't exist, then set the default page 
    $includePage = 'default.php'; # Should reflect the desired default page for steps not matching 1 or 2 
} 

include($includePage); 
?> 
関連する問題