Wrong percentage in rating system

Discussion in 'PHP' started by Divvy, Aug 13, 2017.

  1. #1
    Hello guys,

    Can someone help me with a code change?

    I use this plugin in my wordpress site:
    https://github.com/lesterchan/wp-postratings

    But something is wrong with the thumbs voting.

    Here goes some tests that I did:

    With Values: -1, +1

    I have talked with the plugin author but he is not offering support for this fix. He said:

    I'm not interested in the star rating, only up/down rating.

    [​IMG]

    I was checking the code in this file:
    https://github.com/lesterchan/wp-postratings/blob/master/wp-postratings.php

    And I have found this:
    if($post_ratings_score == 0 || $post_ratings_users == 0) {
            $post_ratings = 0;
            $post_ratings_average = 0;
            $post_ratings_percentage = 0;
        } else {
            $post_ratings = round($post_ratings_average, 1);
            $post_ratings_percentage = round((($post_ratings_score/$post_ratings_users)/$ratings_max) * 100, 2);
        }
    Code (markup):
    Is this the right code to fix this? Is it hard to found a solution?
     
    Divvy, Aug 13, 2017 IP
  2. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    I have checked the code and this wrong % showing I think is because of when user dislike the vote value increment by 1 added to the total

    for eg. at this:
    $post_ratings_percentage = round((($post_ratings_score/$post_ratings_users)/$ratings_max) * 100);
    PHP:
    $post_ratings_score=+2 like -2 dislike

    $post_ratings_users =total votes

    $ratings_max= 2

    if 5 vote up and 1 down the this function calculate

    (8/6/2)*100) = 66 %
    but if we don't add 1 when dislike to total rated user then this function work good

    (8/5/2)*100)=80%

    but for fix this i still not find any solution. Am I close?
     
    Divvy, Aug 14, 2017 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Pseudocode, not based on your real variables just verbose meanings.

    $percent = ($votes_up / ($votes_up + $votes_down)) * 100;

    This assumes $votes_down is a positive number, if negative subtract instead of add. Remember, - a - is a +

    You just need to divide the votes up by the total number of up and down.
     
    deathshadow, Aug 17, 2017 IP
    Divvy likes this.
  4. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Thank you bud, worked perfectly :)
     
    Divvy, Aug 17, 2017 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Oh, and don't forget to trap zero votes manually -- divide by zero errors can drive you nutters.
     
    deathshadow, Aug 18, 2017 IP