Calculate percentage of votes [closed]

0

Hello, I have a news voting system that contains like and deslike.

However, I would like to calculate a percentage of this news, based on the number of votes, positive or negative. For example, when you have no vote it would be 100%, or with 10 positives also 100%. How can I do this in php?

    
asked by anonymous 19.06.2017 / 22:09

1 answer

5

Well, I believe that's it:

EDIT:

According to Bacco's comment, there is a risk of division by zero that can be inhibited with an IF. This way:

$like = 3;
$dislike = 10;
$total = $like + $dislike;
if($total != 0){
  $porcentagemLike = ($like / $total) * 100;
  $porcentagemDislike = ($dislike / $total) * 100;
} else {
#caso nao tenha like ou dislike, seta like como 100% e dislike como zero;
  $porcentagemLike = 100;
  $porcentagemDislike = 0;

}
    
19.06.2017 / 23:18