我有这个评级系统,每页一个项目可以正常工作,但我想更改它,以便可以评定多个项目。
每个项目的渲染html都是这样的 - data-id=""
包含唯一的ID:
<div class="ratings">
<span class="ratingAverage" data-av="0"></span>
<span class="item" data-id="7"></span>
<div class="bar">
<span class="bg" style="width: 96%;"></span>
<span class="stars">
<span class="star full" data-vote="1">
<span class="starimg"></span>
</span>
<span class="star full" data-vote="2">
<span class="starimg"></span>
</span>
<span class="star full" data-vote="3">
<span class="starimg"></span>
</span>
<span class="star full" data-vote="4">
<span class="starimg"></span>
</span>
<span class="star" data-vote="5">
<span class="starimg"></span>
</span>
</span>
</div>
<div class="votes"><span>12 votes</span></div>
</div>
JS:
// Rating
var average = $('.ratingAverage').attr('data-av');
function getAvg(average) {
average = (Number(average) * 20);
$('.bg').css('width', 0);
$('.bar .bg').animate({
width: average + '%'
}, 500);
}
getAvg(average);
$('.star').on('mouseover', function() {
var indexAtual = $('.star').index(this);
for (var i = 0; i <= indexAtual; i++) {
$('.star:eq(' + i + ')').addClass('full');
}
});
$('.star').on('mouseout', function() {
$('.star').removeClass('full');
});
$('.star').on('click', function() {
var itemId = $('.item').attr('data-id');
var vote = $(this).attr('data-vote');
$.post('vote.php', {
vote: 'yes',
item: itemId,
point: vote
}, function(data, status) {
getAvg(data.average);
var suffix = (data.votes == 1)
? "vote"
: "votes";
$('.votes span').html(data.votes + " " + suffix);
}, 'jSON');
});
但是这个.js为所有项目设置了相同的投票。如何为每件商品收集average
并在每件商品上运行getAvg(average)
?
vote.php:
<?php
// Connect to db here.
if(isset($_POST['vote'])){
$id = (int)$_POST['item'];
$points = (int)$_POST['point'];
$query = $pdo->prepare("SELECT votes, rating FROM ". $DBtable ." WHERE `id` = ?");
$query->execute(array($id));
while($row = $query->fetchObject()){
$pointsUpdate = ($row->rating+$points);
$voteUpdate = ($row->votes+1);
$updateQuery = $pdo->prepare("UPDATE ". $DBtable ." SET `votes` = ?, `rating` = ? WHERE `id` = ?");
if($updateQuery->execute(array($voteUpdate, $pointsUpdate, $id))){
$avg = round(($pointsUpdate/$voteUpdate),1);
die(json_encode(array('average' => $avg, 'votes' => $voteUpdate)));
} else {
echo "Could not update votes";
}
}
}
?>
我希望这段代码足以证明这个问题。 非常感谢你提前。
答案 0 :(得分:0)
Transaction::where(function($q) {
$q->where('seller_id', $this->id)
->orwhere('buyer_id', $this->id)
->whereIn('concept', ['Lemonway', 'Paypal']);
})
->where('status', 'ok')
->get();