Elizabeth Kof
Elizabeth Kof

Reputation: 65

Catch parameter from URL, pass parameter to a function, execute that function

I have seen a case where for example a URL like this http://example.com/?edit=39 straight away opens a page where you can edit only the article/post which has ID = 39. I have read a lot about calling a function via URL but I have never met the case where you can catch parameter from the URL, pass the parameter into the function and then execute the function.

Using URL http://example.com/?edit=39 as example, how do I use a function to identify 39 as the ID and retrieve a post from the database which has ID = 39.

Upvotes: 1

Views: 100

Answers (2)

Naren Verma
Naren Verma

Reputation: 2307

For example your URL is test.php?edit=39

if (isset($_GET['edit'])) {// it will catch the 39
    $id= $_GET['edit'];
     $return_value=funciton_name($id);// passing parameter to the function
     echo $return_value;// it will display the return vlaue.
}

//execute that function and return the value

function funciton_name($id){
    $result1 = "SELECT * FROM tablename WHERE id=".$id;
    return $result1;
}

Upvotes: 1

seravee
seravee

Reputation: 434

Use parse_url() and parse_str() functions.

For example

$parts = parse_url($url); parse_str($parts['query'], $query); echo $query['email'];

If you want to get the $url dynamically have a look at this below question: Get the full URL in PHP

Upvotes: 1

Related Questions