Resgastar id of the news that I am clicking?

0

I have a mobile app with a zone where you can set up various news through ng-repeat . Now I have a taste system of every news item where you have a button to make the news tick.

What happens now is that I have to get the id of the news and I'm getting it, but when I like it in a news story it rescues the id from other news and not the one I clicked on.

PHP

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Content-type: application/json");

require_once("../funcoes/funcoes.php");


$result_posts_home = $conexao->prepare("SELECT * FROM posts_home WHERE activo = :activo ");
$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,
        'url_artigo' => $row_posts_home->url_artigo,
    );

}

$obj = new stdClass();
$obj->result = $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.result;

        angular.forEach(data.result, function(value, key){
           $partilharRecursos.set('idNoticia', value.id);
        });   
    });
})

View

 <div class="row" ng-repeat="noticias in noticias_home">
        <div class="col">
            <a href="#/app/ver-noticia/{{noticias.url_artigo}}/{{noticias.id}}">
                <div style="background: url(https://www.sabeonde.pt/gtm/anexos/posts_home/{{noticias.id_anexo}}.{{noticias.tipo}}); border-top-left-radius:10px; border-top-right-radius:10px; height: 200px; background-size: 100% 100%; background-repeat: no-repeat;">
                </div>
            </a>
            <div style="border-bottom-left-radius:10px; border-bottom-right-radius:10px; height: 100px; background-color: white;">
                <table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td colspan="2" valign="top">
                            <a href="#/app/ver-noticia/{{noticias.url_artigo}}/{{noticias.id}}"><div style="font-size: 15px; color:black; margin:5px 0px 15px 10px;  font-weight: bold; ">{{noticias.titulo}}</div></a>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top">
                           <div ng-init="liked='Gosto'" ng-click="like()" ng-controller="LikeNoticiasHome" style="margin-left:10px;" class="botao_gosto"><i class="fa fa-heart"></i> {{liked}}</div>
                           <div id="mostra_gostos" class="mostra_gostos">{{noticias.likes}}</div>
                           <a onclick="window.plugins.socialsharing.share('{{noticias.titulo}}', 'SabeOnde', 'https://www.sabeonde.pt/gtm/anexos/posts_home/{{noticias.id_anexo}}.{{noticias.tipo}}', 'https://www.sabeonde.pt/{{noticias.url_artigo}}')" href=""><div class="botao_posts"><i class="fa fa-share-alt"></i> Partilhar</div></a>
                           <a href="#/app/ver-noticia/{{noticias.url_artigo}}/{{noticias.id}}"><div class="botao_posts"><i class="fa fa-search"></i> Ver +</div></a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>

Controller Like

.controller('LikeNoticiasHome', function($scope, $http, $stateParams, sessionService, $partilharRecursos) {
    var hasLiked = false;
    $scope.like= function (){

        if (!hasLiked) {
            hasLiked = true;
            $scope.liked = 'Não Gosto';
            $scope.likeCount += 1;

            $http.get("https://www.sabeonde.pt/api/api_like_noticias.php?post_id="+$partilharRecursos.get("idNoticia")+ "&user_id=" + sessionService.get('user_id')).success(function (data) {
                $scope.like_noticias_home = data;
            });

        } else {
            hasLiked = false;
            $scope.liked = 'Gosto';
            $scope.likeCount -= 1;

            $http.get("https://www.sabeonde.pt/api/api_unlike_noticias.php?post_id="+$partilharRecursos.get("idNoticia")+ "&user_id=" + sessionService.get('user_id')).success(function (data) {
                $scope.like_noticias_home = data;
            });
        }     
    }
})
    
asked by anonymous 12.10.2015 / 23:44

3 answers

0

From what I can see, you simply pass id into your scope, and process your method for it:

.controller('LikeNoticiasHome', function($scope, $http, $stateParams, sessionService, $partilharRecursos) {
    var hasLiked = false;
    $scope.like= function (id) {

      if (!hasLiked) {
         hasLiked = true;
         $scope.liked = 'Não Gosto';
         $scope.likeCount += 1;

         $http.get("https://www.sabeonde.pt/api/api_like_noticias.php?post_id=" + id + "&user_id=" + sessionService.get('user_id')).success(function (data) {
            $scope.like_noticias_home = data;
         });

      } else {
        hasLiked = false;
        $scope.liked = 'Gosto';
        $scope.likeCount -= 1;

        $http.get("https://www.sabeonde.pt/api/api_unlike_noticias.php?post_id=" + id + "&user_id=" + sessionService.get('user_id')).success(function (data) {
            $scope.like_noticias_home = data;
        });
      }  

    }
});

And in your view:

<div class="row" ng-repeat="noticias in noticias_home">
        <div class="col">
            <a href="#/app/ver-noticia/{{noticias.url_artigo}}/{{noticias.id}}">
                <div style="background: url(https://www.sabeonde.pt/gtm/anexos/posts_home/{{noticias.id_anexo}}.{{noticias.tipo}}); border-top-left-radius:10px; border-top-right-radius:10px; height: 200px; background-size: 100% 100%; background-repeat: no-repeat;">
                </div>
            </a>
            <div style="border-bottom-left-radius:10px; border-bottom-right-radius:10px; height: 100px; background-color: white;">
                <table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td colspan="2" valign="top">
                            <a href="#/app/ver-noticia/{{noticias.url_artigo}}/{{noticias.id}}"><div style="font-size: 15px; color:black; margin:5px 0px 15px 10px;  font-weight: bold; ">{{noticias.titulo}}</div></a>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top">
                           <div ng-init="liked='Gosto'" ng-click="like({{noticias.id}})" ng-controller="LikeNoticiasHome" style="margin-left:10px;" class="botao_gosto"><i class="fa fa-heart"></i> {{liked}}</div>
                           <div id="mostra_gostos" class="mostra_gostos">{{noticias.likes}}</div>
                           <a onclick="window.plugins.socialsharing.share('{{noticias.titulo}}', 'SabeOnde', 'https://www.sabeonde.pt/gtm/anexos/posts_home/{{noticias.id_anexo}}.{{noticias.tipo}}', 'https://www.sabeonde.pt/{{noticias.url_artigo}}')" href=""><div class="botao_posts"><i class="fa fa-share-alt"></i> Partilhar</div></a>
                           <a href="#/app/ver-noticia/{{noticias.url_artigo}}/{{noticias.id}}"><div class="botao_posts"><i class="fa fa-search"></i> Ver +</div></a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    
13.10.2015 / 14:52
1

Good morning Great, it would be possible for you to post the code of your function like () that is in the controller that you did in the angle, probably the problem is in it, before you post it here, you can try to change it, put it without catching the news id by the scope and yes passing it as parameter in the function and treating it independently within the like () function.

In case with like when you do this

$scope.like= function (id){
...//cod
}

This function when called in the view will have to receive a parameter, and in the ng-click of your view, more precisely in the div where you control the like,

ng-click="like(noticia.id)"

So you pass the ID of the clicked news.

In case I also noticed that you are treating $ scope.liked as a general variable, it would be interesting not to work with it so, because if you enter a value for it, that value will be used across the scope, in this case To simplify this, you could use the function like by getting as a parameter a new variable, for example:

<div>... ng-click="like(noticia)"...


   .controller('LikeNoticiasHome', function($scope, $http, $stateParams, sessionService, $partilharRecursos) {
   // var hasLiked = false;
$scope.like= function (noticiaClicada){

    if (!noticia.hasLiked) {
        noticiaClicada.hasLiked = true;
        $scope.noticiaClicada.liked = 'Não Gosto';
        $scope.likeCount += 1;

        $http.get("https://www.sabeonde.pt/api/api_like_noticias.php?post_id="+$partilharRecursos.get("idNoticia")+ "&user_id=" + sessionService.get('user_id')).success(function (data) {
            $scope.like_noticias_home = data;
        });

    } else {
        noticiaClicada.hasLiked = false;
        $scope.noticiaClicada.liked = 'Gosto';
        $scope.likeCount -= 1;

        $http.get("https://www.sabeonde.pt/api/api_unlike_noticias.php?post_id="+$partilharRecursos.get("idNoticia")+ "&user_id=" + sessionService.get('user_id')).success(function (data) {
            $scope.like_noticias_home = data;
        });
    }     
}
})

Ok, in case you can try to use ng-repeat directly in your table's code.

<tr ng-repeat="noticia in noticias">
<td colspan="2" valign="top">
    <a href="#/app/ver-noticia/{{noticia.url_artigo}}/{{noticia.id}}"><div style="font-size: 15px; color:black; margin:5px 0px 15px 10px;  font-weight: bold; ">{{noticia.titulo}}</div></a>
</td>
                </tr>
                <tr>
                    <td valign="top">
                       <div ng-init="liked='Gosto'" ng-click="like(noticia.id)" ng-controller="LikeNoticiasHome" style="margin-left:10px;" class="botao_gosto"><i class="fa fa-heart"></i> {{liked}}</div>
                       <div id="mostra_gostos" class="mostra_gostos">{{noticia.likes}}</div>
                       <a onclick="window.plugins.socialsharing.share('{{noticia.titulo}}', 'SabeOnde', 'https://www.sabeonde.pt/gtm/anexos/posts_home/{{noticia.id_anexo}}.{{noticias.tipo}}', 'https://www.sabeonde.pt/{{noticia.url_artigo}}')" href=""><div class="botao_posts"><i class="fa fa-share-alt"></i> Partilhar</div></a>
                       <a href="#/app/ver-noticia/{{noticias.url_artigo}}/{{noticia.id}}"><div class="botao_posts"><i class="fa fa-search"></i> Ver +</div></a>
                    </td>
                </tr>

In this case, I just picked up that array that you fed into foreach php and put it to feed variables into the table, now it should no longer get the wrong news id, but in this case the foreach it used in the controller. it will no longer be necessary ok.

    
13.10.2015 / 13:57
0

As you're calling like () within a ng-repeat , I recommend passing the id from ng -repeat function for the like () function. It is common to have this kind of problem because in the loop repetition angular isolates the scopes.

It would look something like this:

angular.module("myApplication", [])
  .controller("myCtrl", function($scope) {
    $scope.notices = [];
    $scope.notices.push({id: 1, name: "AAA"});
    $scope.notices.push({id: 2, name: "BBB"});
    $scope.notices.push({id: 3, name: "CCC"});

    $scope.like = function(notice_id) {
      alert('Id da notícia: ' + notice_id);
    };
  });
<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script><metacharset="utf-8">
  <title>JS Bin</title>
</head>

<body>
  <div ng-app="myApplication" ng-controller="myCtrl">
    <h2>POSTS</h2>
    <table>
      <tr>
        <th>ID</th>
        <th>NAME</th>
      </tr>
      <tr ng-repeat="notice in notices">
        <td ng-bind="notice.id"></td>
        <td ng-bind="notice.name"></td>
        <td>
          <button type="button" ng-click="like(notice.id)">like</button>
        </td>
      </tr>
    </table>
  </div>
</body>

</html>
    
13.10.2015 / 14:08