As a beginner in angularJS, I am trying to display all customers from MySQL. Here is the code I have written in the controller and service:
app.controller('CustomersController', function ($scope, customersService, $http) {
init();
function init() {
$scope.customers = customersService.getCustomers();
}
});
app.service('customersService', function ($http) {
this.getCustomers = function () {
return customers;
};
// The issue lies here
$http.get("app/server/read.php")
.success(function(data){
var customers = data;
});
});
In my PHP script, I have the following code:
$result = mysqli_query($con,"SELECT * FROM customers");
$return_arr = array();
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
$rowArr = array(
'id' => $row['id'],
'firstName' => $row['firstname'],
'lastName' => $row['lastname'],
'address' => $row['address'],
'city' => $row['city']
);
$return_arr[] = $rowArr;
}
echo json_encode($return_arr);
The JSON array returned by PHP looks like this:
[{"id":"36","firstName":"asdasd","lastName":"asdasd","address":"","city":"asdasd"},{"id":"37","firstName":"asdasd","lastName":"asdasd","address":"","city":"asdasd"},{"id":"38","firstName":"asdasd","lastName":"asdasd","address":"","city":"asdasd"},{"id":"39","firstName":"","lastName":"","address":"","city":""},{"id":"40","firstName":"asd","lastName":"asd","address":"","city":"asd"}]
I am struggling to assign this JSON object array to the variable customers
dynamically after the successful response of the GET method.