2016-07-31 2 views
1

각도 및 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 
    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

= 배열 ​​(); 그때? –

+0

@ B.Kocaman 나는 내가 어떻게해야할까요? – tanyaa

답변

0

당신은 아래 대신에만 responseresponse.data을 얻어야한다 :

$scope.customersDetails = response.data; 

또한 ngRepeat 지시문 대신 ngOptions을 사용하는 것이 좋습니다.

0

PHP 파일에 몇 가지 문제가 있습니다.

1- 고객 등급이 없습니다. (이 예제에서는 필요 없다고 생각합니다)

2- $ queryResult2 = array(); 배열을 만들었지 만 데이터로 피드하지 않았습니다.

3 빈 배열을 인코딩했습니다.

4 인코딩 된 데이터를 인코딩하려고 시도했습니다.

고정 예를 확인해주세요 : 문제는 PHP 코드의 $의 queryResult2에

<?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; 
?> 
관련 문제