-1
Hello, everyone. I am studying and doing some programming tests with PHP + Angularjs. I searched everywhere, but I couldn’t print in an html table the array generated by PHP.
Can anyone help me? I’ll be very grateful!
The code goes 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;
});
});
php users.
<?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 users.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]"}]}
To register that his function worked, simply select his answer as "accept" http://meta.pt.stackoverflow.com/questions/452/question-j%C3%A1-has-defined-by%C3%A9m-n%C3%A3o-has-been-marked-as-accepted? Rq=1
– MarceloBoni