Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm new to Angular JS. I'm able to call a php file and get the data. But, now the scenario is that I need to call a particular method from a php and get the data using "$http.get". Take a look at my code, whether I'm calling the method in the correct way or not?

Angular Code

    // Ajax call for listing countries.
    var countryPromise = $http.get("ListData.php/getCountries()");
    // Create global users array.
    $scope.countriesArray = [];

    countryPromise.success(function(data, status, headers, config) {
        for(index in data) {
            alert(data[index].name);
            $scope.countriesArray.push({
                id:data[index].id,
                name:data[index].name                   
            });
        }
    });
    countryPromise.error(function(data, status, headers, config) {
        alert("Loading countries failed!");
    });

PHP

<?php
class ListData
{
function __construct() {

    // credentials of MySql database.
    $username = "root";
    $password = "admin";
    $hostname = "localhost"; 

    $countryData = array();
    //connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password)
      or die("Unable to connect to MySQL");

    $selected = mysql_select_db("Angular",$dbhandle)
    or die("Could not select Angular");
}
public function getCountries() {

    //execute the SQL query and return records
    $result = mysql_query("SELECT id,name FROM Country");

    //fetch tha data from the database
    while ($row = mysql_fetch_array($result)) {

        $id = $row{'id'};
        $name = $row{'name'};
        $countryData[] = array('id' => $id, 'name' => $name);
    }
    echo json_encode($countryData); 
}

} ?>

share|improve this question
    
You are using a framework or something? Maybe a routing mechanism? How is your mechanism to get the method getCountries from the class ListData from a HTTP Request? You have to explain that to us. I don't think its a problem related to Angular. –  Emanuel Gianico Apr 4 at 4:08
    
I'm not using any framework. I'm simply creating a php file and running it through localhost using apache server. It works fine when I route to a php file. But, its not working when I call to a method. I guess routing mechanism is not a problem. Correct me if I'm wrong. –  Nizam Apr 4 at 4:10
    
Well, you cannot call a class method from a HTTP Request in that way. You have to use a routing class for that. –  Emanuel Gianico Apr 4 at 4:12
    
@EmanuelGianico, can you plz explain with a small snippet or example? –  Nizam Apr 4 at 4:13
    
yes, i added it below. Cheers! –  Emanuel Gianico Apr 4 at 4:38

2 Answers 2

up vote 0 down vote accepted

You need to know what is the route of the php function getContries and then call this route from the $http.get function.

Do you hard coded your php server or are you using a framework ?

share|improve this answer
    
Hi Eliel, I'm just running the php in Apache. But, it goes succesfully when I use simple PHP file. Now, I want to use different methods for different calls which I may use later, in order to keep only one PHP file. –  Nizam Apr 4 at 4:06
    
What do you mean by " it goes succesfully when I use simple PHP file"? what is the url you tap to access the getCountries() function without angularjs ? –  Eliel Haouzi Apr 4 at 4:13
    
the url itself is ListData.php –  Nizam Apr 4 at 4:13
    
Is ListData.php url without parameter call to getCOuntries() function ? In this case you need to call in your $http.get only to ListData.php without the "/getCountries()" suffix. –  Eliel Haouzi Apr 4 at 4:19
    
Then how can I point in JS, that the result should get from getCountries() function? It's totally confusing me. The JQuery Ajax is much simpler. –  Nizam Apr 4 at 4:20

I think that you are misunderstanding the way that PHP works. Actually you are trying to call a method from a class with an HTTP Request. Thats is not possible atleast not without a routing class.

The routing class its just a class that intercepts all the HTTP Request to analyze the URI, and based on a pattern, it match to a preexisting class, instantiate (create) it and call the wanted method from that class.

For example, our routing class have a pattern like this:

Class/Method

an intercept a Http Request like this:

www.oursite.com/ListData/getCountries

Where ListData is our class, and getCountries is our method or action.

So, the routing class just do this:

$Class->Method();

Of course we can achieve to pass parameters, and intercept and routing specific http request type's like: post, get, update, delete, etc.

There's a lot of frameworks who make that for you, but if you only want to use the Routing, here's a couple

PHP Frameworks that I recommend (because I worked with them):

In anycase, if you don't want nothing of that, you could just create a folder called ListData and inside it create a file called getCountries.php. On that file just put the code to instantiate your class and call your method.

<?php
include('../ListData.php');
$cIns = new ListData();
$cIns->getCountries();
?>

In that way, it gonna work in the way you are calling the url (don't forget to add the .php extension at the end (: )

share|improve this answer
    
Excellent. Totally makes sense. Thank you. –  Nizam Apr 4 at 4:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.