2

I have been trying to retrieve data from mysql. So, I have written in php:

$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);

I want to push this data into an array of angularjs success:

var customers = [];

$http.get("app/server/read.php")
    .success(function(data){
        customers.push(data);
        //console.log(data);
    });

If I send single row from mysql, it receives i.e. when I write in php echo json_encode($rowArr);, then var customers able to receives otherwise can't for multiple rows. In the console I get:

[Object { id="36", firstName="asdasd", lastName="asdasd", more...}, Object { id="37", firstName="asdasd", lastName="asdasd", more...}, Object { id="38", firstName="asdasd", lastName="asdasd", more...}, Object { id="40", firstName="asd", lastName="asd", more...}, Object { id="41", firstName="asdasd", lastName="asdasd", more...}, Object { id="42", firstName="asdasd", lastName="asdas", more...}, Object { id="43", firstName="asdasd", lastName="asdasd", more...}]

Please could you help me anyone that where I am doing wrong?

UPDATE:

html:

<div class="col-lg-3 card" data-ng-repeat="customer in customers | orderBy:'lastName'">
    <button class="btn close cardClose" data-ng-click="deleteCustomer(customer.id)">&times;</button>
    <div class="cardHeader">{{customer.firstName + ' ' + customer.lastName}}</div>
    <div class="cardBody">{{customer.city}}</div>
</div>

Controller:

app.controller('CustomersController', function ($scope, customersService, $http) {
init();

function init() {
    $scope.customers = customersService.getCustomers();
 }
});

Service:

app.service('customersService', function ($http) {
    this.getCustomers = function () {
    return customers;
};

var customers = [];
$http.get("app/server/read.php")
    .success(function(data){
        //customers.push(data);
        customers = data;
    });
});
2

1 Answer 1

2

Try this:

app.service('customersService', function ($http, $q) {
   var deferred = $q.defer();
   $http.get('app/server/read.php').then(function(res) {
     deferred.resolve(res);
   });

   var getCustomers = function() {
     return deferred.promise;   
   };

   return {
     getCustomers: getCustomers
   };
});

And in your controller:

 customersService.getCustomers().then(function(customers) {
    $scope.customers = customers;
 });
7
  • no, in case he wants to push more values instead of replacing, this code will fail. Commented Mar 30, 2014 at 9:00
  • var customers = []; is the line before it, and the title says "into empty array in angularjs" so I doubt that is what he is trying to do. Commented Mar 30, 2014 at 9:01
  • Yes I tried to do as customers = data; but no luck. I have also doubt var customers = []; but do not make any sense what should be there? Commented Mar 30, 2014 at 9:08
  • I don't see what the problem is - when you console.log it shows you are getting the data, right? Commented Mar 30, 2014 at 9:12
  • yes, console.log shows the data but I do not see any output in browser. If I write something hard-coded into that array i.e. var customers = [{ id: 1, firstName: 'Lee', lastName: 'Carroll', address: '1234 Anywhere St.', city: 'Phoenix' }] it works as well as when I send any single row from mysql that works too. Commented Mar 30, 2014 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.