XNOR
XNOR

Reputation:

How to: PHP dynamic url checking

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?

Example:

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 a separate file named edit and use only one tier of the query string, but I would like to know how to do this.

Thanks in advance!

Upvotes: 0

Views: 1154

Answers (3)

Galen
Galen

Reputation: 30170

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

Upvotes: 5

Rickster
Rickster

Reputation: 220

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

Upvotes: 2

Gumbo
Gumbo

Reputation: 655189

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

if (isset($_GET['edit'])) {
    echo 'edit mode';
    if (isset($_GET['n'])) {
        echo 'editing '.intval($_GET['n']);
    }
}

Upvotes: 4

Related Questions