phpget-method

How can I get $_GET values to a variable


In my page, there are multiple $_GET values. ie

if(isset($_GET["projects"]))
{ .....    }
else if(isset($_GET["research"]))
{ ...... }
else if(isset($_GET["publication"]))
{ ..... }

...upto 10 elseif's

Can I shorten this? Can I get these values {projects,research, publication,..} in a variable.?


Solution

  • Ok so I guess I figured out what you want from your comments. Lets see.

    $types = array('projects', 'research', 'publication'); // add as many as you want
    $valid = array_intersect_key($_GET, array_flip($types));
    
    if (count($valid) > 1)
        die "More than one category is set, this is invalid.";
    
    if (!$valid)
        die "No category was set, you must choose one.";
    
    foreach ($valid as $type => $value) // just to split this one element array key/value into distinct variables
    {
        $value = mysql_real_escape_string($value); // assuming you are using mysql_*
        $sql = "SELECT * FROM table WHERE $type = '$value'";
    }
    
    ...