Actually your problem must be happening because you are NOT returning the search result as an array, but rather as a simple sql result, that is, the angular will be able to read, but it would not be possible to transcribe to an ng-repeat (or other treatment), since you are creating an invalid json object'.
What you should do is create a php that can transcribe your result in the following format:
[
{id:1,cliente:'nome do cliente 1',estado_civil:'solteiro'..},
{id:2,cliente:'nome do cliente 2',estado_civil:'casado'..},
{id:3,cliente:'nome do cliente 3',estado_civil:'viuvo'..},
[.. etc ..]
]
For this you can use a code similar to this:
function usuarios(){
$data = array();
$qry = sql("select * from users");
$i=0;
foreach($qry as $r) {
foreach ($r as $j=>$k) {
if(!is_int($j)){
$data[$i][$j] = $k;
}
}
$i++;
}
return json_encode($data);
}
I recommend caution when doing this get, especially when it comes to creating a json file. Once you write a new json and write it to the server, it will be available to anyone who can access your folder structure.
With the need for an sql to get the data, your application is safer.
In the part of your Angularjs, it is actually preferable that you use a function with var
instead of $scope
to get that data. By creating a new $Cope, you increase the possibility of errors, as it creates interaction with DOM and increases the need for Angularjs checks.
The only detail of this is that your php result will be like this:
data: [
{id:1,cliente:'nome do cliente 1',estado_civil:'solteiro'..},
{id:2,cliente:'nome do cliente 2',estado_civil:'casado'..},
{id:3,cliente:'nome do cliente 3',estado_civil:'viuvo'..},
[.. etc ..]
]
Then your code in Angularjs, should return to the array DATA
, being like this:
var carregarUsuarios = function () {
$http.get("php/buscar.php").then(function (retorno){
$scope.usuarios = retorno.data;
});
};
Note the definition of $scope.usuarios
, i say I want to assign the 'return' and then call the 'array', ie, retorno.data
.
Complementing even more, if you want to use multiple functions in php, just do the following:
//utilize a url da seguinte maneira
$http.get("php/buscar.php?action=carregarUsuarios")
//ou chamando outra função
$http.get("php/buscar.php?action=maisFuncao")
And Php:
switch($_GET['action']) {
case 'carregarUsuarios': carregarUsuarios();
break;
case 'maisFuncao': maisFuncao();
break;
}
function carregarUsuarios() {
//sua função aqui
}
function maisFuncao() {
//sua função aqui
}
Pg_fetch_assoc would not be from Postgresql and not from MySQL in your vuscar.php file?
– Giancarlo Abel Giulian
I don’t know @Giancarlogiulin, I took this code from another post of this American forum.
– GustavoSevero