php开关没有显示默认情况

时间:2017-02-26 22:42:23

标签: php switch-statement case

我创建了一个php脚本,它使用case和switch来获取动态内容信息。一切正常但我不能让它显示默认页面。我搜索了所有的stackoverflow和谷歌,似乎无法找出为什么我的默认在交换机不会加载。我尝试过其他方法,例如include(' page.php');

<center><div class="dropdown">
  <button class="dropbtn">TICKERS</button>
  <div class="dropdown-content">
    <a href="?page=crz">CRZ</a>
    <a href="?page=fft">FFT</a>
    <a href="?page=ghg">GHG</a>
    <a href="?page=glh">GLH</a>
    <a href="?page=ian">IAN</a>
    <a href="?page=IMH">IMH</a>
    <a href="?page=in">IN</a>
    <a href="?page=lag">LAG</a>
    <a href="?page=lib">LIB</a>
    <a href="?page=lxx">LXX</a>
    <a href="?page=mdm">MDM</a>
    <a href="?page=mj">MJ</a>
    <a href="?page=mmj">MMJ</a>
    <a href="?page=nf">NF</a>
    <a href="?page=puf">PUF</a>
    <a href="?page=SL">SL</a>
    <a href="?page=sun">SUN</a>
    <a href="?page=tbp">TBP</a>
    <a href="?page=thc">THC</a>
    <a href="?page=tny">TNY</a>
    <a href="?page=vgw">VGW</a>
    <a href="?page=vp">VP</a>
    <a href="?page=vrt">VRT</a>
   </div>
</div></center>

<BR>

<?php 
        if(isset($_GET['page']) && $_GET['page']!=""){
            $page = "";
            switch ($_GET['page']) {
                case 'crz':
                    $page = "crz.php";
                    break;

                case 'fft':
                    $page = "fft.php";
                    break;

                case 'ghg':
                    $page = "ghg.php";
                    break;

                case 'glh':
                    $page = "glh.php";
                    break;

                case 'ian':
                    $page = "ian.php";
                    break;

                case 'in':
                    $page = "in.php";
                    break;

                case 'lag':
                    $page = "lag.php";
                    break;

                case 'lib':
                    $page = "lib.php";
                    break;

                case 'lxx':
                    $page = "lxx.php";
                    break;

                case 'mdm':
                    $page = "mdm.php";
                    break;

                case 'mj':
                    $page = "mj.php";
                    break;

                case 'mmj':
                    $page = "mmj.php";
                    break;                    

                case 'nf':
                    $page = "nf.php";
                    break;

                case 'puf':
                    $page = "puf.php";
                    break;

                case 'sl':
                    $page = "sl.php";
                    break;

                case 'sun':
                    $page = "sun.php";
                    break;

                case 'tbp':
                    $page = "tbp.php";
                    break;

                case 'thc':
                    $page = "thc.php";
                    break;

                case 'tny':
                    $page = "tny.php";
                    break;

                case 'vgw':
                    $page = "vgw.php";
                    break;

                case 'vp':
                    $page = "vp.php";
                    break;

                case 'vrt':
                    $page = "vrt.php";
                    break;

                default:
                    $page = "csegainers.php";  
                    break;


            }

            include($page);
        }
     ?>

3 个答案:

答案 0 :(得分:3)

你在if条件下写到:

if(isset($_GET['page']) && $_GET['page']!="")

这意味着:如果没有输入任何内容,则根本不要查看switch语句。

在此处添加else分支或删除if-clause。

答案 1 :(得分:1)

这不是你的问题答案,但有所改进:

$allowed = array('fft','ghg','glh','ian','imh',
                'in','lag','lib','lxx','mdm','mj',
                'mmj','nf','puf','sl','sun','tbp',
                'thc','tny','vgw','vp','vrt','crz' , //...
                );  

$page = 'csegainers';

if(isset($_GET['page']) && in_array($_GET['page'],$allowed)){
    $page = $_GET['page'];
}

include($page . '.php');

答案 2 :(得分:0)

初始if不会让任何空白的$ _GET ['page']通过它。如果手动访问不同的值,请说“document.php?page = example”,默认情况下会正常启动。