如何使用AngularJS从PHP文件中提取数据

时间:2017-04-12 03:57:51

标签: php angularjs json http formatting

好的我的php文件有问题。我正在使用AngularJS从php文件中提取一些数据, 这是我的角度代码。它是W3schools的源代码。伟大的代码,但我遇到了一些麻烦,如承诺:

    <!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<ul>
  <li ng-repeat="x in myData">
    {{ x.Name + ', ' + x.Country }}
  </li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
  $http.get("customers.php").then(function (response) {
      $scope.myData = response.data.records;
  });
});
</script>

</body>
</html>

所以,我的问题是如何格式化php来阅读这个

{
  "records": [
    {
      "Name": "Alfreds Futterkiste",
      "City": "Berlin",
      "Country": "Germany"
    },
    {
      "Name": "Ana Trujillo Emparedados y helados",
      "City": "México D.F.",
      "Country": "Mexico"
    },
    {
      "Name": "Antonio Moreno Taquería",
      "City": "México D.F.",
      "Country": "Mexico"
    },
    {
      "Name": "Around the Horn",
      "City": "London",
      "Country": "UK"
    },
    {
      "Name": "B's Beverages",
      "City": "London",
      "Country": "UK"
    },
    {
      "Name": "Berglunds snabbköp",
      "City": "Luleå",
      "Country": "Sweden"
    },
    {
      "Name": "Blauer See Delikatessen",
      "City": "Mannheim",
      "Country": "Germany"
    },
    {
      "Name": "Blondel père et fils",
      "City": "Strasbourg",
      "Country": "France"
    },
    {
      "Name": "Bólido Comidas preparadas",
      "City": "Madrid",
      "Country": "Spain"
    },
    {
      "Name": "Bon app'",
      "City": "Marseille",
      "Country": "France"
    },
    {
      "Name": "Bottom-Dollar Marketse",
      "City": "Tsawassen",
      "Country": "Canada"
    },
    {
      "Name": "Cactus Comidas para llevar",
      "City": "Buenos Aires",
      "Country": "Argentina"
    },
    {
      "Name": "Centro comercial Moctezuma",
      "City": "México D.F.",
      "Country": "Mexico"
    },
    {
      "Name": "Chop-suey Chinese",
      "City": "Bern",
      "Country": "Switzerland"
    },
    {
      "Name": "Comércio Mineiro",
      "City": "São Paulo",
      "Country": "Brazil"
    }
  ]
}

所有这些都是W3schools.com的形式我正在尝试重现php文件的家庭作业,但可以弄清楚如何格式化php来阅读这个。请帮忙。此外,这里有一个w3school示例的链接,我正在使用https://www.w3schools.com/angular/angular_http.asp它是在JSON示例下。这个例子很棒,但它只有角度部分而不是php部分,我找不到任何信息。

由于

2 个答案:

答案 0 :(得分:0)

把它放在你的文件php

$conn = new mysqli('localhost','root','','project');
$sql="SELECT * FROM ...";
$result = mysqli_query($conn,$sql);

$rows = array();
if($result){
while($r = mysqli_fetch_assoc($result)) {
    $rows[] = $r;
}
print '{"records":' .json_encode($rows) ."}";
}else{
    echo mysqli_error();
}

答案 1 :(得分:0)

试试这个:

      <?php
         $dbhost = 'HOSTNAME';
         $dbuser = 'DBUSERNAME';
         $dbpass = 'DBPASSWORD';
         $dbname = 'DATABASENAME';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

         if(! $conn ) {
            die('Could not connect: ' . mysqli_error());
         }
         $sql = 'SELECT * FROM tbl_name';
         $result = mysqli_query($conn, $sql);

         if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
               $rows[] = $row;
            }
            print "{'records':" .json_encode($rows) ."}";
         } else {
            echo "0 results";
         }
         mysqli_close($conn);
      ?>