angularjs with php

 
php with angularjs

index.html

<!-- Learn AngularJS-->
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

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

<table border="2">
  <tr ng-repeat="x in names">
    <td>{{ x.First }}</td>
    <td>{{ x.Last_Name }}</td>
     <td>{{ x.Email }}</td>
  </tr>
</table>

</div>

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

</body>
</html>

customers_mysql.php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "root", "", "mydatabase");
$result = $conn->query("SELECT *  FROM contacts");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}
    $outp .= '{"First":"'  . $rs["contact_first"] . '",';
    $outp .= '"Last_Name":"'   . $rs["contact_last"]        . '",';
    $outp .= '"Email":"'. $rs["contact_email"]     . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo($outp);
?>
php with angularjs


EmoticonEmoticon