Jump to content

Best way to handle multiple $_GETs?


Aureole

Recommended Posts

This is an overly simplified example of what I am actually doing but you get the idea. I bet there's a better way to do this...

 

The code doesn't really make sense but you get the idea... I should also probably be checking that there's an act before showing a do or an action like

 

if(isset($_GET['act']) && $_GET['act'] == 'one' && $_GET['do'] == 'something') { ...

 

But that is besides the point, what is the best way of doing this? Should I just use another switch statement for each one or would that not work? Any input is appreciated.

 

<?php
switch($_GET['act']) {
    case 'one';
one();
break;
    case 'two';
two();
break;
    default:
one();
break;
}

function one() {
    if($_GET['do'] == 'something') {
        echo('Something.');
    }
    elseif($_GET['do'] == 'somethingelse') {
        if($_GET['action'] == 'dothis') {
            echo('Do this.');
        }
        elseif($_GET['action'] == 'dothat') {
            echo('Do that.');
        }
        else {
            echo('Something...');
        }
    }
}

function two() {
    // ...
}

?>

 

I already searched Google and couldn't find anything, just so you know... ;)

Link to comment
https://forums.phpfreaks.com/topic/73170-best-way-to-handle-multiple-_gets/
Share on other sites

Probably easiest to setup a seperate switch to deal with every $_GET var. eg;

 

<?php

  if (isset($_GET['action'])) {
    switch ($_GET['action']) {
      // run functions based on action.
    }
  }

  if (isset($_GET['view'])) {
    switch ($_GET['view']) {
      // run functions based on view.
    }
  }

  // etc etc

?>

I tried that then but then when I use functions from my class like $class->function(); within the include I get an error, or maybe I just did it wrong... I don't know I posted in OOP anyway. But at first I thought it was because it was within an include, then I tried it in the file itself and I still got the error.

 

Including isn't a bad solution anyway but it can get a little messy...

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.