0
I have a mobile app, I have a array
to be generated through php what I want now and on angularjs
transform the array into an object because when I want to grab for example the id gives undefenid because this as array
and has to be object I would like to know how I can transform that array
what php asset in an object ?
PHP
$result_posts_home = $conexao->prepare("SELECT * FROM posts_home WHERE activo = :activo ORDER BY id DESC ");
$result_posts_home->bindValue(':activo', 1, PDO::PARAM_INT);
$result_posts_home->execute();
$posts_home = $result_posts_home->fetchAll(PDO::FETCH_OBJ);
foreach ($posts_home as $row_posts_home) {
$result_posts_home_anexos = $conexao->prepare("SELECT * FROM posts_home_anexos WHERE id_mae = :id_mae AND seccao = :seccao ");
$result_posts_home_anexos->bindParam(':id_mae', $row_posts_home->id, PDO::PARAM_INT);
$result_posts_home_anexos->bindValue(':seccao', 'thumbnail', PDO::PARAM_STR);
$result_posts_home_anexos->execute();
$row_posts_home_anexos = $result_posts_home_anexos->fetch(PDO::FETCH_OBJ);
// VALIDA SE USER JA TEM LIKE NO POST
$total_likes = $conexao->prepare("SELECT * FROM likes WHERE id_post = :id_post AND user_id = :user_id ");
$total_likes->bindValue(':id_post', $row_posts_home->id, PDO::PARAM_INT);
$total_likes->bindValue(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$total_likes->execute();
$likes = $total_likes->fetch(PDO::FETCH_ASSOC);
$noticias[] = array(
'id' => $row_posts_home->id,
'id_anexo' => $row_posts_home_anexos->id_anexo,
'tipo' => $row_posts_home_anexos->tipo,
'likes' => $row_posts_home->likes,
);
}
echo json_encode($noticias);
Controller
.controller('ListaNoticiasHome', function($scope, $http, $stateParams, sessionService, $partilharRecursos) {
$http.get("https://www.sabeonde.pt/api/api_noticias_home.php").success(function (data) {
$scope.noticias_home = data;
$partilharRecursos.set('idNoticia', data.id);
});
})
And in php how would you return as I did in the second example ? can edit and how would the php part look like
– César Sousa
You are returning multiple objects, which one you will set to idNoty?
– EProgrammerNotFound
I want the id but it will always undefened and I have various news can not be fetch the first has to fetch automatically
– César Sousa
I didn’t understand your last sentence
– EProgrammerNotFound
but now sends me the id of another news when I click on another news does not send the correct id of the one I am clicking
– César Sousa
Detail better what you need, I can’t help you any more than that with the details provided.
– EProgrammerNotFound
I have an area where I list several news, in these news I have a boot for each to make like what is happening now and that is sending the news id but not the correct news I am clicking
– César Sousa
In which object do you store these id’s? in shareRecourses? because it gets only 1 id and you are returning several in your API. You should know which one to store. The array problem has been solved, maybe it would be case for another question
– EProgrammerNotFound
yes I am storing in the shareRecourse what I want and store here the id of the news that I am clicking
– César Sousa