Jump to content

[SOLVED] Using If statements and $_GET variables


sh0wtym3

Recommended Posts

My script in a nutshell:

 

if ($_GET["cmd"]=="1") { show table 1 };
if ($_GET["cmd"]=="2") { show table 2 };
if ($_GET["cmd"]=="3") { show table 3 };

 

So by going to www.mysite.com?cmd=1 you will see table 1.

 

But I want to show a default table, let's name it table 4, to show when no $_GET variable is set.

 

So by going to www.mysite.com you will see table 4. How do I accomplish this?

 

I thought by simply adding an else statement at the end it would work (shown below), but it doesn't.  :(

 

if ($_GET["cmd"]=="1") { show table 1 };
if ($_GET["cmd"]=="2") { show table 2 };
if ($_GET["cmd"]=="3") { show table 3 };
else { show table 4 }

I would use a case/switch.

 

<?php
$cmd = isset($_GET['cmd'])?$_GET['cmd']:0;

switch($cmd) {
    default:
    case "0":
        //show default table 
    break;
    case "1":
        //show table 1
    break;
    case "2":
        //show table 2 
    break;
}

?>

Do it like this:

 

switch ($_GET[cmd])

{

default: include "table4file"; break;

case '1': include "table1file"; break;

case '2': include "table1file"; break;

case '3': include "table1file"; break;

}

 

If your Tables are in the same file or come from a function replace include "filename"; with function-name();

 

 

Similar to what got posted above me, i was to slow =X

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.