CoipsyAllesee
New Member
I'm trying to display the average rate and how many votes have been casted but I really don't know how to do it. I need help on how to display something like this \[code\]Rating. 4.83/5 (6725 vote cast)\[/code\] under the star rating? But I really don't know how to do this or what to add or change?Here is the JavaScript below.\[code\]// 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"); } }); });});\[/code\]
And here is the php script below.
\[code\]<?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 retrievefunction 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 ratingfunction rate(){ $text = strip_tags($_GET['rating']); $update = "update vote set counter = counter + 1, value = http://stackoverflow.com/questions/2004642/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); }}?>\[/code\]Here is my MySQL table.\[code\]CREATE TABLE IF NOT EXISTS `vote` (`counter` int(8) NOT NULL default '0',`value` int(8) NOT NULL default '0')\[/code\]Here is the rating display markup below.\[code\]<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>\[/code\]
And here is the php script below.
\[code\]<?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 retrievefunction 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 ratingfunction rate(){ $text = strip_tags($_GET['rating']); $update = "update vote set counter = counter + 1, value = http://stackoverflow.com/questions/2004642/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); }}?>\[/code\]Here is my MySQL table.\[code\]CREATE TABLE IF NOT EXISTS `vote` (`counter` int(8) NOT NULL default '0',`value` int(8) NOT NULL default '0')\[/code\]Here is the rating display markup below.\[code\]<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>\[/code\]