0

I already searched lot of stuff but it doesn't help. I want my data from php to be like this :

$scope.places = [{name: 'John'},{name: 'Jane'}];

My problem is I dont know how to achieve this thing. Here'e my angularjs looks like:

$scope.getNames = function(){
     $http.post('get',{}).then(function(response){
            $scope.places = response.data;
     });
};

$scope.getNames();

PHP

$sql = "SELECT * FROM tblplace";
$res = $con->query($sql);
while($row = mysqli_fetch_array($res)){
    // code here.
}

HTML

<select class="form-control places">
    <option value="empty">Select</option>
    <option ng-repeat="place in places" value="{{place.name}}">  {{place.name}}</option>
 </select>

How to do it? Thanks!

1 Answer 1

1

You need to use json_encode in your php output.

$json_array = array();
$sql = "SELECT * FROM tblplace";
$res = $con->query($sql);
while($row = mysqli_fetch_array($res)){
   $temp_arr['name'] = $row['name'];
   $json_array[] = $temp_arr;
}
echo json_encode($json_array);

Adjust the while loop accordingly. Haven't used php so I'm a bit fuzzy on the syntax.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help. :D

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.