2016-07-31 16 views
1

私はangleとphpでプロジェクトを構築しています。私は自分のデータベースに "顧客"テーブルを持っています。ドロップリストの "Customers"テーブルからすべての名前を取得しようとしていますが、動作していません。今のところコンソールにエラーは表示されません。誰も私のコードをチェックしてくださいできますか?は、角度とPHPを使ってドロップリストでデータを取得できません。

HTML:

  <select> 
       <option ng-repeat="x in customersDetails track by $index" value="{{x.customer_id}}" {{x.full_name}}</option> 
      </select> 
     </div> 

コントローラー:

"use strict"; 

angular.module('dataSystem').controller('priceOfferCtrl', function($scope,$route,$location,$http) 
{ 
    $http({method:'GET', url:'api/customers-tab/get-priceOffer.php/'}) 
     .success(function(response) { 

     $scope.customersDetails = response; 
     }) 

     // This will log you the error code and trace, if there is an error. 

     .catch(function(err) { 
     console.log('err', err) 
     }); 
}); 

PHPの

<?php 
    header('Content-Type: text/html; charset=utf-8'); 
    $connection = mysqli_connect('localhost','root','','hamatkin'); 

    mysqli_query($connection,"SET character_set_client = utf8"); 
    mysqli_query($connection,"SET character_set_connection = utf8"); 
    mysqli_query($connection,"SET character_set_results = utf8"); 

    if(!$connection){ 
    die("couldnt connect".mysqli_error); 
    } 
    $customer = new Customer(); 
    $query = "SELECT * FROM `customers``"; 
    $queryResult = $connection->query($query); 
    $queryResult2 = array(); 
    if($queryResult->num_rows>0){ 
    while($row = $queryResult->fetch_assoc()){ 
     $customer->customer_id = $row['customer_id'] 
     $customer->full_name =$row['full_name'] 

    } 
    } 
    header('Content-type:application/json'); 
    $queryResult3 = json_encode($queryResult2); 
    echo json_encode($queryResult3); 
?> 
+0

問題はPHPのコード$ queryResult2である=配列();それで? –

+0

@ B.Kocaman申し訳ありませんが、私はこれで何ができるのですか? – tanyaa

答えて

0

あなたは以下のように、代わりにのみresponseresponse.dataを取得する必要があります:

$scope.customersDetails = response.data; 

また、ngRepeatディレクティブの代わりにngOptionsを使用することをお勧めします。

0

PHPファイルにはいくつか問題があります。

1-お客様のクラスはありません。 (私はあなたがこの例では必要ないと思う)

2 $ queryResult2 = array();配列を作成しましたが、データをフィードしませんでした。

3空の配列をエンコードしました。

4エンコードされたデータをエンコードしようとしました。

一定の例をしてくださいチェック:

<?php 
    header('Content-Type: text/html; charset=utf-8'); 
    $connection = mysqli_connect('localhost','root','','hamatkin'); 

    mysqli_query($connection,"SET character_set_client = utf8"); 
    mysqli_query($connection,"SET character_set_connection = utf8"); 
    mysqli_query($connection,"SET character_set_results = utf8"); 

    if(!$connection){ 
    die("couldnt connect".mysqli_error); 
    } 

    /* 
    I dont know if you have 'Customer' class  
    */ 
    // $customer = new Customer(); 

// $query = "SELECT * FROM `customers`"; 
    $query = "SELECT customer_id, full_name FROM `customers`"; 
    $queryResult = $connection->query($query); 
    $queryResult2 = array(); 
    if($queryResult->num_rows>0){ 
    $i = 0; 
    while($row = $queryResult->fetch_assoc()){ 
     // if you dont have Customer class, there is no $customer->.... 
    // $customer->customer_id = $row['customer_id'] 
     // $customer->full_name =$row['full_name'] 

    // put the data in $queryResult2 
    // $queryResult2[$i]['customer_id'] = $row['customer_id']; 
    // $queryResult2[$i]['full_name'] = $row['full_name']; 
// OR 
$queryResult2[] = $row; 

    $i++; 
    } 
    } 

    header('Content-type:application/json'); 
    $queryResult3 = json_encode($queryResult2); 
    // echo json_encode($queryResult3); // you don't need to encode again. 
    echo $queryResult3; 
?> 
関連する問題