JQuery&;PHP-如何显示和获得平均投票率和投票?

JQuery&;PHP-如何显示和获得平均投票率和投票?,php,jquery,mysql,Php,Jquery,Mysql,我试图显示平均投票率和投了多少票,但我真的不知道怎么做 我需要关于如何显示类似于评级的内容的帮助。4.83/5(6725票投票)在明星级别下?但我真的不知道该怎么做,也不知道该添加或更改什么 下面是JavaScript // JavaScript Document $(document).ready(function() { // get current rating getRating(); // get rating function function get

我试图显示平均投票率和投了多少票,但我真的不知道怎么做

我需要关于如何显示类似于
评级的内容的帮助。4.83/5(6725票投票)
在明星级别下?但我真的不知道该怎么做,也不知道该添加或更改什么

下面是JavaScript

// JavaScript Document
$(document).ready(function() {
    // get current rating
    getRating();
    // get rating function
    function getRating(){
        $.ajax({
            type: "GET",
            url: "update.php",
            data: "do=getrate",
            cache: false,
            async: false,
            success: function(result) {
                // apply star rating to element
                $("#current-rating").css({ width: "" + result + "%" });
            },
            error: function(result) {
                alert("some error occured, please try again later");
            }
        });
    }

    // link handler
    $('#ratelinks li a').click(function(){
        $.ajax({
            type: "GET",
            url: "update.php",
            data: "rating="+$(this).text()+"&do=rate",
            cache: false,
            async: false,
            success: function(result) {
                // remove #ratelinks element to prevent another rate
                $("#ratelinks").remove();
                // get rating after click
                getRating();
            },
            error: function(result) {
                alert("some error occured, please try again later");
            }
        });

    });
});

<?php
// connect to database
$dbh=mysql_connect ("localhost", "root", "", "sitename") or die ('Cannot connect to the database');
mysql_select_db ("sitename",$dbh);

if($_GET['do']=='rate'){
    // do rate
    rate();
}else if($_GET['do']=='getrate'){
    // get rating
    getRating();
}

// function to retrieve
function getRating(){
    $sql= "select * from vote";
    $result=@mysql_query($sql);
    $rs=@mysql_fetch_array($result);
    // set width of star
    $rating = (@round($rs[value] / $rs[counter],1)) * 20; 
    echo $rating;
}

// function to insert rating
function rate(){
    $text = strip_tags($_GET['rating']);
    $update = "update vote set counter = counter + 1, value = value + ".$_GET['rating']."";

    $result = @mysql_query($update); 
    if(@mysql_affected_rows() == 0){
        $insert = "insert into vote (counter,value) values ('1','".$_GET['rating']."')";
        $result = @mysql_query($insert); 
    }
}
?>
下面是php脚本

// JavaScript Document
$(document).ready(function() {
    // get current rating
    getRating();
    // get rating function
    function getRating(){
        $.ajax({
            type: "GET",
            url: "update.php",
            data: "do=getrate",
            cache: false,
            async: false,
            success: function(result) {
                // apply star rating to element
                $("#current-rating").css({ width: "" + result + "%" });
            },
            error: function(result) {
                alert("some error occured, please try again later");
            }
        });
    }

    // link handler
    $('#ratelinks li a').click(function(){
        $.ajax({
            type: "GET",
            url: "update.php",
            data: "rating="+$(this).text()+"&do=rate",
            cache: false,
            async: false,
            success: function(result) {
                // remove #ratelinks element to prevent another rate
                $("#ratelinks").remove();
                // get rating after click
                getRating();
            },
            error: function(result) {
                alert("some error occured, please try again later");
            }
        });

    });
});

<?php
// connect to database
$dbh=mysql_connect ("localhost", "root", "", "sitename") or die ('Cannot connect to the database');
mysql_select_db ("sitename",$dbh);

if($_GET['do']=='rate'){
    // do rate
    rate();
}else if($_GET['do']=='getrate'){
    // get rating
    getRating();
}

// function to retrieve
function getRating(){
    $sql= "select * from vote";
    $result=@mysql_query($sql);
    $rs=@mysql_fetch_array($result);
    // set width of star
    $rating = (@round($rs[value] / $rs[counter],1)) * 20; 
    echo $rating;
}

// function to insert rating
function rate(){
    $text = strip_tags($_GET['rating']);
    $update = "update vote set counter = counter + 1, value = value + ".$_GET['rating']."";

    $result = @mysql_query($update); 
    if(@mysql_affected_rows() == 0){
        $insert = "insert into vote (counter,value) values ('1','".$_GET['rating']."')";
        $result = @mysql_query($insert); 
    }
}
?>
下面是评级显示标记

<ul class='star-rating'>
  <li class="current-rating" id="current-rating"><!-- will show current rating --></li>
  <span id="ratelinks">
  <li><a href="javascript:void(0)" title="1 star out of 10" class="one-star">1</a></li>
  <li><a href="javascript:void(0)" title="2 stars out of 10" class="two-stars">2</a></li>
  <li><a href="javascript:void(0)" title="3 stars out of 10" class="three-stars">3</a></li>
  <li><a href="javascript:void(0)" title="4 stars out of 10" class="four-stars">4</a></li>
  <li><a href="javascript:void(0)" title="5 stars out of 10" class="five-stars">5</a></li>
  <li><a href="javascript:void(0)" title="6 stars out of 10" class="six-star">6</a></li>
  <li><a href="javascript:void(0)" title="7 stars out of 10" class="seven-stars">7</a></li>
  <li><a href="javascript:void(0)" title="8 stars out of 10" class="eight-stars">8</a></li>
  <li><a href="javascript:void(0)" title="9 stars out of 10" class="nine-stars">9</a></li>
  <li><a href="javascript:void(0)" title="10 stars out of 10" class="ten-stars">10</a></li>
  </span>
</ul>

您不应该使用同步请求。使用回调异步处理响应。此外,要获得星级评定的好方法,请查看以下问题: