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 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;
    });
});
share|improve this question
    
take a look at this stackoverflow.com/questions/4156101/… –  Khanh TO Mar 30 at 8:59
    
I used concat but does not work –  StreetCoder Mar 30 at 9:09
add comment

1 Answer

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;
 });
share|improve this answer
    
no, in case he wants to push more values instead of replacing, this code will fail. –  Khanh TO Mar 30 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. –  dave Mar 30 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? –  StreetCoder Mar 30 at 9:08
    
I don't see what the problem is - when you console.log it shows you are getting the data, right? –  dave Mar 30 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. –  StreetCoder Mar 30 at 9:20
show 4 more comments

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.