0
I have a mobile app with an area where I list several news through the ng-repeat
. Now I have a taste system (like) of every news where there’s a button to make the news taste.
What happens now is I have to get the id
of the news and I’m already getting it, but when I like a news story he rescues the id
of another news and not the one I clicked.
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;
});
}
}
})
Could make available the view of controller Listanoticiashome?
– Daniel
I edited the question and put it there
– César Sousa
You have to pass the news id in the click method and capture in your controller the reference in the database. It’s the same thing when you send the route via get... it’s no different than that.
– Ivan Ferrer
How I catch is what I don’t understand
– César Sousa