Skip to content

Instantly share code, notes, and snippets.

@seanmcn
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanmcn/4e52ffa582175c362046 to your computer and use it in GitHub Desktop.
Save seanmcn/4e52ffa582175c362046 to your computer and use it in GitHub Desktop.
Returning a contestants percentage of votes
<?php
/* Gets the count of all votes for a specific poll */
public function totalVoteCount($pollId) {
$this->db->select("*");
$this->db->where("poll_id", $pollId);
$count = $this->db->count_all_results('votes');
return $count;
}
/* Returns the percentage of votes a contestant has in a specific poll */
public function getContestantPercentage($pollId, $contestantId) {
$this->db->select("*");
$this->db->where("poll_id", $pollId);
$this->db->where("contestant_id", $contestantId);
//Get the contestants votes
$contestantVotes = $this->db->count_all_results('votes');
//Get the total amount of votes
$allVotes = $this->totalVoteCount($pollId);
if($contestantVotes != 0) {
$percentage = $contestantVotes/$allVotes;
$percentageString = number_format( $percentage * 100, 0) . '%';
return $percentageString;
}
else {
return "0%";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment