PHP mysql星级

时间:2015-08-22 20:17:40

标签: php jquery mysql ajax

我按照教程here发现,我似乎无法弄清楚如何从数据库中的评分中填充星标。基本上,我想要在用户提交他/她自己的评级之前显示当前的评级和数量。 我不想动态构建我需要评级的产品列表,我只需要发布id并按照数据ID生成评级计数......

- 更新我可以发布评分,但我需要为每个产品分配一个ID。我可以只获取一个值的值,但我认为我需要分配一个var,以便我可以为任何给定的产品提取它们

产品:

<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>

1 个答案:

答案 0 :(得分:0)

假设您只能评价1个产品(正如演示所做的那样)。要获得当前评级,您可以选择所有评级并对其进行平均:

Current rating: SELECT AVG(rate) FROM tbl_rating
Number of ratings: SELECT COUNT(*) FROM tbl_rating

现在,如果您需要了解当前用户的评分:

SELECT rate FROM tbl_rating WHERE user_id = $userIPAdress

如果您向product_id表添加tbl_rating列以便能够为多个产品评分,只需添加一个WHERE子句:

Current rating: SELECT AVG(rate) FROM tbl_rating WHERE product_id = $productId
Number of ratings: SELECT COUNT(*) FROM tbl_rating WHERE product_id = $productId