Husam Al-Masri
Husam Al-Masri

Reputation: 31

Pass parameter to function using URL

This is the function i tried to call it from url

 public function getParentId($childId)
 {
    $statment = $this->db->prepare("SELECT parent FROM `person` WHERE id = $childId");
    $statment->execute();
    $result = $statment->fetchAll();

    foreach($result as $output){

        return $output['parent'];
    }
    echo 'You call the function from url';

 }

and i wrote this code to access the function from url

 if(isset($_GET['action'])){
    include_once('Family.php');
    $object = new Family;

    switch($_GET['action']){
        case 'getId':
        $object->getParentId($childId);
        break;

        default;
        echo 'There is no function with that name';
        break;
    }
}

then i pass the argument from the Url

test.local/Family.php?action=getId&childId=4

the output You call the function from url

but he didn't return $output['parent'] why ?

Upvotes: 2

Views: 72

Answers (1)

Issam Rafihi
Issam Rafihi

Reputation: 432

I think you meant to write the second snippet of code like this:

if(isset($_GET['action'])){
    include_once('Family.php');
    $object = new Family;

    switch($_GET['action']){
        case 'getId':
        $object->getParentId($_GET['childId']); //<= Edit
        break;

        default;
        echo 'There is no function with that name';
        break;
    }
}

Upvotes: 2

Related Questions