Hello everyone. I am studying and doing some programming tests with PHP + AngularJS. I looked at everything, but I could not print the PHP-generated array in a html table.
Can anyone help me? I will be very grateful!
The code is below.
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<script src="js/jquery.min.js"></script>
<script src="js/angular.min.js"></script>
<script src="js/angular-script.js"></script>
</head>
<body ng-app="App" ng-controller="AppCtrl">
<div>
<table>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Email</th>
</tr>
<tr ng-repeat="detail in details">
<td>{{detail.id}}</td>
<td>{{detail.nome}}</td>
<td>{{detail.email}}</td>
</tr>
</table>
</div>
</body>
</html>
angular-script.js
var App = angular.module('App',[]);
App.controller('AppCtrl',function($scope, $http){
$http.post("usuarios.php").success(function(data){
$scope.details = data;
});
});
users.php
<?php
$con = mysqli_connect("127.0.0.1", "root", "", "banco");
$query = "SELECT * from tab_usuarios";
$result = mysqli_query($con, $query);
if(mysqli_num_rows($result) != 0) {
$i = 0;
$arr = array('usuarios' => array());
while($row = mysqli_fetch_assoc($result)){
$arr['usuarios'][$i]['id'] = utf8_encode($row['id']);
$arr['usuarios'][$i]['nome'] = utf8_encode($row['nome']);
$arr['usuarios'][$i]['email'] = utf8_encode($row['email']);
$i++;
}
echo json_encode($arr);
?>
When I run Usuarios.php, it returns the following array:
{"usuarios":[{"id":"1","nome":"Paulo Oliveira","email":"[email protected]"},{"id":"2","nome":"Olivia Pereira","email":"[email protected]"},{"id":"3","nome":"Lucio Costa","email":"[email protected]"}]}