Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
in Q2A Core by

Sometimes, the absolute number makes less senes than the percentage. For example, if a post have 10 votes, 4 up, 6 down. I would like to display something like "40% up". 


1 Answer

+3 votes

First, read up on advanced themes if you haven't already.

You will need to override the vote_count function. You should be able to change this line:

} else
$this->output_split($post['netvotes_view'], 'qa-netvote-count');

to this:

} else {
$percent = $post['upvotes_raw'] / ($post['upvotes_raw'] + $post['downvotes_raw']);
$this->output_split( $percent.'%', 'qa-netvote-count');

Now, this should work, but I have no idea how it will function with the Javascript AJAX upvoting/downvoting, it may mess things up a bit. TBH saying the percentage of upvotes doesn't really tell you how popular something is, because something with 1 upvote (100%) isn't more popular than something with 9 upvotes and 1 downvote (90%).

edited by
Great, DisgruntledGoat,

You pointed in the right direction. However, output_split somehow does not work. So I made some modifications for it to output in the right format and to detect a div/0 error.

} else {
if( ($post['upvotes_raw'] + $post['downvotes_raw'])!=0 )
$percent = number_format(100 * $post['upvotes_raw'] / ($post['upvotes_raw'] + $post['downvotes_raw']),0, ".", " ");
$this->output($percent.'% up');
else $this->output( 'not enough data available');

Thanks again. GS