2017-03-26 24 views
0

私はすでにたくさんのものを検索しましたが、助けになりません。私は私のデータをこのようになるようにします:angjjsのphpからjsonデータを取得する方法

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

私の問題は、このことを達成する方法は分かりません。それを行うにはどのように

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

:Here'e私angularjsは次のようになりますか?ありがとう!

答えて

1

PHPの出力でjson_encodeを使用する必要があります。

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

whileループを適切に調整します。 phpを使用していないので、私は構文上少しばかりです。

+0

ありがとうございました!できます! – duterte

+0

うれしい私は助けることができます。 :D –

関連する問題