我正在關注發現的教程here,我似乎無法弄清楚如何從db中的收視率中填充星星。基本上,我想要在用戶提交自己的評級之前顯示目前的評分和計數顯示多少評級。 我不想動態建立我需要的評級的產品列表,我只需要ID發佈和評級計數生成每個數據ID ...PHP mysql star rating
--UPDATE我能夠發佈的評級,但我需要爲每個產品分配一個ID。我能得到的值只是一個價值,但我想我需要分配一個變種,這樣我可以拉他們對於任何給定的產品
產品:
<div class="store-item-rating text-warning rate-ex1-cnt" id ="1">
<div id="1" class="rate-btn-1 rate-btn"></div>
<div id="2" class="rate-btn-2 rate-btn"></div>
<div id="3" class="rate-btn-3 rate-btn"></div>
<div id="4" class="rate-btn-4 rate-btn"></div>
<div id="5" class="rate-btn-5 rate-btn"></div>
</div>
<div class="store-item-rating text-warning rate-ex2-cnt" id ="2">
<div id="1" class="rate-btn-1 rate-btn"></div>
<div id="2" class="rate-btn-2 rate-btn"></div>
<div id="3" class="rate-btn-3 rate-btn"></div>
<div id="4" class="rate-btn-4 rate-btn"></div>
<div id="5" class="rate-btn-5 rate-btn"></div>
</div>
的JS
$(function(){
$('.rate-btn').hover(function(){
$('.rate-btn').removeClass('rate-btn-hover');
var therate = $(this).attr('id');
for (var i = therate; i >= 0; i--) {
$('.rate-btn-'+i).addClass('rate-btn-hover');
};
});
$('.rate-btn').click(function(){
var therate = $(this).attr('id');
var dataRate = 'act=rate&product_id=<?php echo $product_id; ?>&rate='+therate; //
$('.rate-btn').removeClass('rate-btn-active');
for (var i = therate; i >= 0; i--) {
$('.rate-btn-'+i).addClass('rate-btn-active');
};
$.ajax({
type : "POST",
url : "inc/ajax.php",
data: dataRate,
success:function(){}
});
});
});
計數
<div class="box-result-cnt">
<?php
$query = mysql_query("SELECT * FROM tbl_rating");
while($data = mysql_fetch_assoc($query)){
$rate_db[] = $data;
$sum_rates[] = $data['rate'];
}
if(@count($rate_db)){
$rate_times = count($rate_db);
$sum_rates = array_sum($sum_rates);
$rate_value = $sum_rates/$rate_times;
$rate_bg = (($rate_value)/5)*100;
}else{
$rate_times = 0;
$rate_value = 0;
$rate_bg = 0;
}
?>
<hr>
<h3>The content was rated <strong><?php echo $rate_times; ?></strong> times.</h3>
<hr>
<h3>The rating is at <strong><?php echo $rate_value; ?></strong> .</h3>
<hr>
<div class="rate-result-cnt">
<div class="rate-bg" style="width:<?php echo $rate_bg; ?>%"></div>
<div class="rate-stars"></div>
</div>
<hr>
</div>
您會告訴我們您到目前爲止嘗試過的嗎? – Schwern