Hi All! I'm currently trying to use a WP plugin that's no longer supported. It's a great plugin, but a bit buggy. One of the issues is the "star rating" system that is used. If you tell the plugin you want to cap out at a 5 star rating, it will display 6 stars on the page. Unfortunately, I haven't figured out where to fix that part of the code. Here is the PHP that appears to create the star system in the plugin: //############################################# //============================================= //== == //== Rating Display Functions == //== == //============================================= //############################################# //================================================== // Returns HTML For Inputting Ratings // When Commenting //================================================== function frev_get_rating_fields($ip_address = "1", $template = "") { global $ratings_cats_table, $ratings_table, $options, $wpdb, $post, $userdata, $image_width; if ($template == "") { $template = $options['input_comment_template']; } get_currentuserinfo(); $sql = "SELECT rating_value FROM `" . $ratings_table . "` w, (SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID = '".$post->ID."' AND (comment_author_IP='".$ip_address."'"; if (isset($userdata->ID)) { //Check Member ID if logged in, to be sure they didn't change IP address $sql .= " OR user_ID=" . $userdata->ID; } $sql .= ")) x Where w.comment_ID=x.comment_ID LIMIT 1;"; $author_IP = $wpdb->get_var($sql); if ($author_IP != '') { //There is a duplicate IP for this post return $options['post_already_rated']; } $sql = "SELECT `id`, `cat`, `scale_min`, `scale_max` FROM `" . $ratings_cats_table . "` ORDER BY `display_order`"; $sql_result = mysql_query($sql); $imgId = 0; while ($row = mysql_fetch_assoc($sql_result)) { $rating = ""; $imgId += 1; $data = str_replace("%c", $row['cat'], $template); if ($options['rating_display'] == 1) { //Display the images if javascript is enabled on the client's browser $rating .= "<div class='ratingcontainer' id='ratingcontainer$imgId' style='width: ".($row['scale_max']*$image_width)."px; float: left; visibility: hidden;'>"; $rating .= "<div class='current-rating' id='current-rating$imgId' "; if ($options['allow_no_rating'] != 1) //If "No Rating" is not allowed $rating .= "style='width: ".($row['scale_min']*$image_width)."px'"; //Since there is no "No Rating" allowed, we have to start the images at the minimum $rating .= "></div>"; for ($i = $row['scale_min']; $i <= $row['scale_max']; $i++) { $rating .= "<a href='javascript:rateImg($i, $imgId);' title='$i out of ". $row['scale_max']. "' class='star$i'> </a>"; } $rating .= "</div>"; $rating .= '<div style="float: right;"><select id="frev'.$imgId.'" name="frev_rating[' . $row['id'] . ']">'; if ($options['allow_no_rating'] == 1) { $rating .= '<option value="-999">No Rating</Option>'; } for ($i = $row['scale_min']; $i <= $row['scale_max']; $i++) { $rating .= '<option value="' . $i . '">' . $i . '</option>'; } $rating .= '</select></div>'; $rating .= "<script language='Javascript'> setDifference(" . ($options['allow_no_rating']-1) . "); showImages($imgId); </script>"; } else { $rating .= '<select name="frev_rating[' . $row['id'] . ']">'; if ($options['allow_no_rating'] == 1) { $rating .= '<option value="-999">No Rating</Option>'; } for ($i = $row['scale_min']; $i <= $row['scale_max']; $i++) { $rating .= '<option value="' . $i . '">' . $i . '</option>'; } $rating .= '</select>'; } $html .= str_replace("%r",$rating,$data); } return $html; } //============================================= // Returns Formatted Ratings To Be Shown // With The Comment Text //============================================= function frev_get_comment_ratings($comment_id, $template = "", $only = "") { global $ratings_cats_table, $ratings_table, $options, $wpdb, $image_width; if ($template == "") { $template = $options['output_comment_template']; } $sql = "SELECT `cat`, `rating_value` FROM `" . $ratings_cats_table . "` " . "INNER JOIN `" . $ratings_table . "` " . "ON (`" . $ratings_cats_table . "`.`id` = `" . $ratings_table . "`.`rating_cat_id`) " . "WHERE `" . $ratings_table . "`.`comment_id` = " . $comment_id . " "; if ($only != "") { $sql .= "AND `" . $ratings_cats_table . "`.`cat` = '" . $only . "' "; } $sql .= "ORDER BY `" . $ratings_cats_table . "`.`display_order`"; $sql_result = mysql_query($sql) or die("Error in: " . $sql); while ($row = mysql_fetch_assoc($sql_result)) { $output = str_replace("%c", $row['cat'], $template); $scale_max = $wpdb->get_var("SELECT `scale_max` FROM `" . $ratings_cats_table . "` WHERE `cat`='" . $row['cat'] . "'"); if ($options['comment_rating_display'] == 1) { $rating = "<a title='" . number_format($row['rating_value'], 1, '.', '') . "/$scale_max'>"; $rating .= "<div class='ratingcontainer'style='width: ".($scale_max*$image_width)."px'>"; $rating .= "<div class='current-rating' style='width: ".($row['rating_value']*$image_width)."px'> </div>"; $rating .= "</div></a>"; } else { $rating = number_format($row['rating_value'], 1, '.', '') . "/" . $scale_max; } $html .= str_replace("%r", $rating, $output); } if ($options['auto_embed_useful'] == 1) { $html .= frev_display_useful($comment_id); } return $html; } PHP: Any insight here would be greatly appreciated. Like I said, this is a great plugin, but it needs a few bugs worked out. Thanks! Marceia
for ($i = $row['scale_min']; $i <= $row['scale_max']; $i++) { ... this code can rate -N to +N, if you assume scale_min is 0 0 would normally mean "no value, no image" but in this code construct 0 is a possible value for the $i counter so it outputs a star.
thanks, Juust. I assume it's that way so someone could leave a zero star rating and you would still see the blank stars... Is there a way to overcome this? I've seen it working elsewhere, just not sure how to fix this guy's code. I'm no php programmer by any stretch. thanks again!
The 'no rating option' is -999, so I'd opt for the 'easy' fix : replace for ($i = $row['scale_min']; $i <= $row['scale_max']; $i++) { PHP: with for ($i = 1; $i <= $row['scale_max']; $i++) { PHP: where it occurs. That should get you a basic 1-N star vote, you still have the -999 'no rating' option, but no '0 stars' vote, hence no problems with the surplus star?