Last active
August 29, 2015 14:10
-
-
Save seanmcn/4e52ffa582175c362046 to your computer and use it in GitHub Desktop.
Returning a contestants percentage of votes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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