Reputation: 53
I`m wondering is it possible to hit specific function (just function,not hole file) with $http service?My $http service looks like this:
$http.get('../classes/githubusers.php').then(function(response){
$scope.gitUsers = response.data;
console.log($scope.gitUsers);
});
And backend API looks like this:
//function to show users on Admin
public function showGithubUsers(){
$sql = "SELECT * FROM githubusers ORDER BY id DESC";
$connect = new Database($username="",$password="");
$connect->connect();
$result = $connect->db->query($sql);
while($row = $result -> fetch(PDO::FETCH_ASSOC)){
$jsonVar[] = $row;
}
echo json_encode($jsonVar);
//var_dump($jsonVar);
}
//function to show unique users from dB
public function showUniqueUsers(){
$sql = "SELECT * FROM githubusers";
$connect = new Database($username="",$password="");
$connect->connect();
$result = $connect->db->query($sql);
$nameArray = [];
while($row = $result -> fetch(PDO::FETCH_ASSOC)){
$this -> name = $row['name'];
array_push($nameArray, $this -> name);
}
$uniqueNameArray = [];
$uniqueNameArray = array_unique($nameArray);
echo json_encode($uniqueNameArray);
}
}
And what i want do is to take data separately from these two PHP functions,but $http service taking data from hole githubusers.php file and applying error on UI.I tried to add function name in URL $http.get('../classes/githubusers.php/showUniqueUsers'),but it didn`t work.
Upvotes: 1
Views: 266
Reputation: 316
Calling a function is not possible. However PHP is smart enough. You can create a new file or add code to existing php file which will call the function for you based on what you pass as POST or GET parameters.
For example you can change your js code to:
$http.get('../classes/githubusers.php?show=unique').then(function(response){
$scope.gitUsers = response.data;
console.log($scope.gitUsers);
});
and your PHP Code becomes
//Can use a switch case here
if($_GET['show'] == 'unique')
showUniqueUsers();
else if ($_GET['show'] == 'github')
showGithubUsers();
//function to show users on Admin
public function showGithubUsers(){
$sql = "SELECT * FROM githubusers ORDER BY id DESC";
$connect = new Database($username="",$password="");
$connect->connect();
$result = $connect->db->query($sql);
while($row = $result -> fetch(PDO::FETCH_ASSOC)){
$jsonVar[] = $row;
}
echo json_encode($jsonVar);
//var_dump($jsonVar);
}
//function to show unique users from dB
public function showUniqueUsers(){
$sql = "SELECT * FROM githubusers";
$connect = new Database($username="",$password="");
$connect->connect();
$result = $connect->db->query($sql);
$nameArray = [];
while($row = $result -> fetch(PDO::FETCH_ASSOC)){
$this -> name = $row['name'];
array_push($nameArray, $this -> name);
}
$uniqueNameArray = [];
$uniqueNameArray = array_unique($nameArray);
echo json_encode($uniqueNameArray);
}
}
Upvotes: 1
Reputation: 11551
Angular http
service is pointing to an accessible url, which means normally in the backend you have to use a controller based mechanism, where you can sort out each kind of action you want to happen when you access an url. Each of existing php framework can do this.
If you are creating your custom php file either you need to use php classes with the basic __construct()
function which is going to be called automatically, or if you don't want to use classes then you need to put the functions into separate php files and called them separately.
Upvotes: 1