views:

339

answers:

3

I am making an admin panel for a small project. I want to use dynamic URLs to edit specific data entries. For instance:

file.php?edit&n=53

I want this URL to edit entry 53.

I use a switch statement to check for the edit page, but how do I check whether or not the URL has the &n=x extension in the same switch statement?

Ex:

switch $_SERVER['QUERY_STRING']
{
    case "edit"
        //shows a list of entries to edit  
        break;
}

Would I just make another case with a reg expression? How would I make said expression?

I realize I could just make seperate file named edit and use only one tier of query string, but I would like to know how to do this.

Thanks in advance!

+4  A: 

Check if $_GET['edit'] and $_GET['n'] exist:

if (isset($_GET['edit'])) {
    echo 'edit mode';
    if (isset($_GET['n'])) {
        echo 'editing '.intval($_GET['n']);
    }
}
Gumbo
+2  A: 

you should be using the $_GET to track variables passed in through the URL.

you can check to see if a variable exists by using isset($_GET['edit']) with isset($_GET['n']) for the page

Rickster
+5  A: 

Like everyone else said use $_GET

I recommend modifying your urls so they look like...

file.php?action=edit&n=53

now you can...

$id = intval( $_GET['n'] );

switch( $_GET['action'] ) {

    case 'edit':
        // Edit entry
        break;
    case 'delete':
        // Delete entry
        break;
    case 'create':
        // Create new entry
        break;
    default:
        // Invalid action

}

PHP page on $_GET - http://us.php.net/manual/en/reserved.variables.get.php

Galen
also look into $_POST and $_REQUEST which differ slightly from $_GET
KOGI