//angularjs_DB.php
<?php
$con=mysqli_connect("localhost","root","","hris_iit");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT idemployee, current_designation,current_salary,previous_designation,previous_salary,reason,effective_date from promotion";
$result = $con->query($sql);
// output data of each row
$output="[";
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
if($output!="["){$output .=",";}
$output .='{"idemployee":"' .$row["idemployee"] .'",';
$output .='"current_designation":"' .$row["current_designation"] .'",';
$output .='"current_salary":"' .$row["current_salary"] .'",';
$output .='"previous_designation":"' .$row["previous_designation"] .'"}';
//echo "<br> id: ". $row['idemployee']. $row['current_designation']. $row['current_salary']. $row['previous_designation'];
}
$output .="]";
mysqli_close($con);
echo ($output);
//return ($output);
?>
<!DOCTYPE html>
<html>
<head>
<script src="angular.min.js">
</script>
</head>
<body>
<div data-ng-app="" data-ng-controller="customerController">
<table>
<tr>
<td><b>idemployee</b></td>
<td><b>current_designation</b></td>
<td><b>current_salary</b></td>
<td><b>previous_designation</b></td>
</tr>
<tr data-ng-repeat="x in names">
<td>{{x.idemployee}}</td>
<td>{{x.current_designation}}</td>
<td>{{x.current_salary}}</td>
<td>{{x.previous_designation}}</td>
</tr>
</table>
</div>
<script>
function customerController($scope,$http){
$http.get("http://localhost/startup/angularjs_DB.php")
.success(function(response){$scope.names=response;});
}
</script>
<!-- The code above can also be used to fetch data from a database -->
</body>
<html>
Problem in output of the html code that are not values from Database !!! Output likes following: idemployee current_designation current_salary previous_designation {{x.idemployee}} {{x.current_designation}} {{x.current_salary}} {{x.previous_designation}}
答案 0 :(得分:1)
这是一个演示,演示如何使用PHP http://plnkr.co/edit/p8kDN3Mj1AcmR6ml312H?p=preview
生成的字符串myApp.controller('customerController',function ($scope,$http){
// $http.get("http://localhost/startup/angularjs_DB.php")
// .success(function(response){$scope.names=response;});
$scope.names = JSON.parse('[{"idemployee": 1}, {"idemployee":2}]')
});
我没有写出整个JSON,但你可以看到它是如何工作的
另外,我不编写PHP,所以我不知道怎么做,但你不需要循环结果集。您应该能够将整个集合作为关联数组并将其传递给json_encode