1

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);
        }
     ?>
2
  • Why not use an array instead of a switch? Why are you hurting yourself? Commented Feb 26, 2017 at 22:50
  • No clue what an array is. Its just for a simple script I did what was easiest! Where is an example of a php array used in dynamic content with a menu Commented Feb 26, 2017 at 22:53

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

isset($_GET['page']) && $_GET['page']!="" like so?
Just remove the whole line containing your if-statement (and the matching curly bracket). Your page will work
1

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

Comments

0

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.

2 Comments

I need my index.php to show csegainers.php as default.
At this part of the code: if(isset($_GET['page']) && $_GET['page']!="") Remove this part: && $_GET['page']!="" It'll then accept you to access it with values that'll fit inside default case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.