2

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

NallyRoll
  • 370
  • 1
  • 8
  • 20

1 Answers1

1

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.

Kyra
  • 5,129
  • 5
  • 35
  • 55
  • that's a good idea. I'll try that out and will get back to you ASAP. Thanks for the input. I've edited the OP to show a link to the plugin. – NallyRoll Apr 02 '12 at 20:56
  • I FIGURED IT OUT! Thanks, @Kyra. Setting the counter=1 still allowed the value to increase, yielding skewed results. What ended up working was changing $update in "function to set rating" at the end. The working line looks like this: `$update = "UPDATE 'pd_total_vote' SET counter = 1, value = ".$_GET['rating']." WHERE id='".$id."' ";` Counter always equals 1, and making it so that each new value is not added to the previous ones. I realize this is still a half-fix, but it will do for now. Thanks again. – NallyRoll Apr 02 '12 at 21:22