Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery & PHP - How to display and get the average rate and votes casted?
    primarykey
    data
    text
    <p>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. </p> <p>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?</p> <p>Here is the JavaScript below.</p> <pre><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()+"&amp;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></pre> <p><br /></p> <p>And here is the php script below.</p> <p><br /></p> <pre><code>&lt;?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); } } ?&gt; </code></pre> <p>Here is my MySQL table.</p> <pre><code>CREATE TABLE IF NOT EXISTS `vote` ( `counter` int(8) NOT NULL default '0', `value` int(8) NOT NULL default '0' ) </code></pre> <p>Here is the rating display markup below.</p> <pre><code>&lt;ul class='star-rating'&gt; &lt;li class="current-rating" id="current-rating"&gt;&lt;!-- will show current rating --&gt;&lt;/li&gt; &lt;span id="ratelinks"&gt; &lt;li&gt;&lt;a href="javascript:void(0)" title="1 star out of 10" class="one-star"&gt;1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="javascript:void(0)" title="2 stars out of 10" class="two-stars"&gt;2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="javascript:void(0)" title="3 stars out of 10" class="three-stars"&gt;3&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="javascript:void(0)" title="4 stars out of 10" class="four-stars"&gt;4&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="javascript:void(0)" title="5 stars out of 10" class="five-stars"&gt;5&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="javascript:void(0)" title="6 stars out of 10" class="six-star"&gt;6&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="javascript:void(0)" title="7 stars out of 10" class="seven-stars"&gt;7&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="javascript:void(0)" title="8 stars out of 10" class="eight-stars"&gt;8&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="javascript:void(0)" title="9 stars out of 10" class="nine-stars"&gt;9&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="javascript:void(0)" title="10 stars out of 10" class="ten-stars"&gt;10&lt;/a&gt;&lt;/li&gt; &lt;/span&gt; &lt;/ul&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload