NallyRoll
NallyRoll

Reputation: 360

JQuery & PHP Star Rating simplified

EDIT: The plugin in question is located here.

PHP beginner here using a JQuery Star Rating snippet and have gotten it to work perfectly. My problem is that it is currently configured to count and display the average of many ratings (for public applications). I'm trying to simplify the plugin so that it allows one to set a personal rating (as if rating your own songs in iTunes). The user may update their rating, but no partial stars would ever exist. I've broken the plugin many times trying to get it working, but to no avail. The mysql database exists as follows:

CREATE TABLE IF NOT EXISTS `pd_total_vote` (
  `id` int(11) NOT NULL auto_increment,
  `desc` varchar(50) NOT NULL,
  `counter` int(8) NOT NULL default '0',
  `value` int(8) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

If I can get it working the way I imagine, I wouldn't require both the counter and value columns, simply a single INT column that holds a value between 1 and 5. Currently counter accumulates the number of votes, while value aggregates the ratings. The stars are then displayed using (value/counter)*20 (as a percentage). The PHP is below (original):

<?php

// connect to database

$dbh=mysql_connect ("localhost", "user", "pass") or die ('Cannot connect to the database');
mysql_select_db ("thenally_pd",$dbh);

if($_GET['do']=='rate'){
    rate($_GET['id']);
}else if($_GET['do']=='getrate'){

    // get rating

    getRating($_GET['id']);
}

// get data from table

function fetchStar(){
    $sql = "select * from `pd_total_vote`";
    $result=@mysql_query($sql);
    while($rs = @mysql_fetch_array($result,MYSQL_ASSOC)){
        $arr_data[] = $rs;
    }
    return $arr_data;
}

// function to retrieve

function getRating($id){
    $sql= "select * from `pd_total_vote` where id='".$id."' ";
    $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 set rating

function rate($id){
    $text = strip_tags($_GET['rating']);
    $update = "update `pd_total_vote` set counter = counter + 1, value = value + ".$_GET['rating']."  where id='".$id."' ";
    $result = @mysql_query($update);
}

?>

Thanks for a point in the right direction,

Mike

Upvotes: 2

Views: 2539

Answers (1)

Kyra
Kyra

Reputation: 5407

I am unsure as I have no access to the rating system you are using yet just glancing at what you have I guess you could keep the counter set to 1 (if removing it breaks the jQuery Rating System) and have the value updated by the person so when you fetch it they only see their value (make sure value can't go above 5). That way if the value is set to 5 then it will show 5 because it isn't finding other ratings.... (based on my understanding) You will also have to add a user id so you know which persons rating to fetch (since you want it personal). This depends on how dependent the application is a specific database design.

Upvotes: 1

Related Questions