malatamil 9 Junior Poster

below this link i have a ratting thing. in this its working but here update and disply part.
http://www.namshimoga.com/searchdetails.php?idval=399&maincatid=147&maincateg=Testingmp#details

in another page http://www.namshimoga.com/searchbycat.php?catagory=Testingmp&maincateg=Testingmp&maincatid=147 i want to display only final rating without updation.

its my display part

<div class='headstar'>
                Rate It !
        <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 5 Poor' class='one-star'>1</a></li>
        <li><a href='javascript:void(0)' title='2 stars out of 5 Average' class='two-stars'>2</a></li>
        <li><a href='javascript:void(0)' title='3 stars out of 5 Good' class='three-stars'>3</a></li>
        <li><a href='javascript:void(0)' title='4 stars out of 5 Very Good' class='four-stars'>4</a></li>
        <li><a href='javascript:void(0)' title='5 stars out of 5 Excellent' class='five-stars'>5</a></li>
    </span>
        <input type='hidden' name='id' id='id' value=' $id '>
        <input type='hidden' name='mainid' id='mainid' value=' $maincatid '>
        </ul>
</div>

starratting.js

// JavaScript Document
    $(document).ready(function() {
        // get current rating
        var value = $("#id").val();
        var main = $("#mainid").val();

        getRating();
        // get rating function
        function getRating(){
            $.ajax({
                type: "GET",
                url: "update.php",
                data: "do=getrate&id="+value,

                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&id="+value,
                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");
                }
            });

        });
    });

update.php

<?php
// connect to database
session_start();

/*$dbh=mysql_connect ("localhost", "root", "") or die ('Cannot connect to the database');
mysql_select_db ("beauty_spa",$dbh);*/
include("db.php");
//$id=$_GET['id'];
//echo "id".$id;
if($_GET['do']=='rate'){
    // do rate
    rate();
}else if($_GET['do']=='getrate'){
    // get rating
    getRating();
}

// function to retrieve
function getRating(){
    $id = strip_tags($_GET['id']);
    $mainid = strip_tags($_GET['mainid']);
    $sql= "select * from vote where spaid='$id' and mainCatagoryid='$mainid'";
    $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']);

    $id = strip_tags($_GET['id']);
    $mainid = strip_tags($_GET['mainid']);
    $update = "update vote set counter = counter + 1, value = value + ".$_GET['rating']." where spaid='$id'";

    $result = @mysql_query($update); 
    if(@mysql_affected_rows() == 0){
        $insert = "insert into vote (counter,value,spaid) values ('1','".$_GET['rating']."','$id')";
        $result = @mysql_query($insert); 
    }
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.