【问题标题】:Using switch on $_GET with if将 $_GET 与 if 一起使用
【发布时间】:2013-06-13 10:20:20
【问题描述】:

所以我基本上试图让它像:?p=blabla&dep=blabla

switch($_GET['p'])
{
case 'home':
    include("template/index.html");
    break;
case null:
    include("template/index.html");
    break;
case 'roster':
    include("template/roster.html");
    break;
case 'about':
    include("template/about.html");
    break;
case 'members':
    include("members/index.php");
    break;
}

if(($_GET['p'] == 'about') && ($_GET['dep'] == 'hospital')) 
{
    include("template/hospital.html");
}

当我做 blablabla?p=about&dep=hospital 时,它仍然包括 about.html 和 hospital.html

我该如何解决这个问题?

【问题讨论】:

    标签: loops get switch-statement case break


    【解决方案1】:

    只需将 if 语句放在 switch case 中即可。

    case 'about':
        if ($_GET['dep'] == 'hospital')
            include("template/hospital.html");
        else
            include("template/about.html");
        break;
    

    【讨论】:

      【解决方案2】:

      这正是你所要求的。

      首先你有你的 switch 语句。它看到 $_GET['p'] 中有“about”,因此它将包含该脚本。

      然后你有你的 if 并且 this 也评估为 true,因此它被包含在内。

      要改变这个:

      在您的“关于”情况下添加另一个。

      case 'about':
          if ($_GET['dep'] == 'hospital') break;
          include("template/about.html");
          break;
      

      【讨论】:

        【解决方案3】:

        您的开关在查找 dep=hospital 的行之前处理,因此它甚至会在查找部门之前包含 about.html。

        如果您只想显示 hospital.html,但前提是 p=about 将测试移动到案例中。

        switch($_GET['p'])
        {
        case 'home':
          include("template/index.html");
          break;
        case null:
          include("template/index.html");
          break;
        case 'roster':
          include("template/roster.html");
          break;
        case 'about':
          if(($_GET['dep'] == 'hospital')) {
            include("template/hospital.html");
          } else {
            include("template/about.html");
          }
          break;
        case 'members':
            include("members/index.php");
            break;
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-12
          • 1970-01-01
          相关资源
          最近更新 更多