How to convert json array to an object in Angularjs?

Asked

Viewed 583 times

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);
    });
})

1 answer

0


You are returning an Array. So you should do so:

$http.get("https://www.sabeonde.pt/api/api_noticias_home.php").success(function (data) {
    $scope.noticias_home = data[0];

    $partilharRecursos.set('idNoticia', $scope.noticias_home.id);
});

I call attention to the fact that it returns an array in your API. IS insecure, so remove it from the server side and return the object directly

Note that I did not do any error processing, or to validate whether there is data in the array. Do it according to your need.

Note also that I am assuming that you only need the first element of the array. If this is not the case, I would return json like this:

{"result": [{"id": 1,....}, {"id": 2,....}]}

To format the result like this in php do the following:

$obj = new stdClass();
$obj->result = $noticias;
echo json_encode($obj);

In Angularjs you must iterate these elements and apply your rule as needed:

$http.get("https://www.sabeonde.pt/api/api_noticias_home.php").success(function    (data) {
    $scope.noticias_home = data.result;
    $partilharRecursos.set('idNoticia', data.result[0].id);

    angular.forEach(data.result, function(value, key){
       console.log(key + ': ' + value);
    });        
});
  • And in php how would you return as I did in the second example ? can edit and how would the php part look like

  • You are returning multiple objects, which one you will set to idNoty?

  • I want the id but it will always undefened and I have various news can not be fetch the first has to fetch automatically

  • I didn’t understand your last sentence

  • 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

  • Detail better what you need, I can’t help you any more than that with the details provided.

  • 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

  • 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

  • yes I am storing in the shareRecourse what I want and store here the id of the news that I am clicking

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.