linux1880 Posted July 3, 2011 Share Posted July 3, 2011 I have written a voting script where we can view a total number of votes per choices ? How do i show the voting result as percentage ? Quote Link to comment https://forums.phpfreaks.com/topic/240993-voting-script-in-result-as-percentage/ Share on other sites More sharing options...
wildteen88 Posted July 3, 2011 Share Posted July 3, 2011 For each choice you divide the votes by the total number of votes and then multiple by 100 to get the percentage. Example code // some dummy data to work with $choices[1] = 6; $choices[2] = 5; $choices[3] = 20; // get the total number of votes $total_votes = array_sum($choices); // loop over the choices foreach($choices as $key => $choice) { // work out the percentage $pcent = round(($choice/$total_votes)*100); // display the result echo "Choice{$key}: $choice votes = %$pcent<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/240993-voting-script-in-result-as-percentage/#findComment-1237880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.