Dan Loze
Dan Loze

Reputation: 109

php switch not showing default case

I created a php script that uses a case and switch for dynamic content info. Everything works but I cannot get it to show the default page. I have searched all of stackoverflow and google and cannot seem to find out why my default in the switch wont load. I have tried other methods such as 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);
        }
     ?>

Upvotes: 1

Views: 186

Answers (3)

talkhabi
talkhabi

Reputation: 2759

This is not your question answer but is improved:

$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');

Upvotes: 1

Erick Mota
Erick Mota

Reputation: 69

The initial if won't let any blank $_GET['page'] pass by it. If you manually access a diferent value, say "document.php?page=example" the default case will fire normally.

Upvotes: 0

Psi
Psi

Reputation: 6783

You write there in your if-condition:

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

So that means: If nothing is entered, don't go through the switch statement at all.

Either add an else-branch here or remove that if-clause.

Upvotes: 3

Related Questions