richarro1234 Posted May 8, 2009 Share Posted May 8, 2009 Hey, i have a bit of code that cycles the DB getting the amount of votes for a survey. <?$query2 = mysql_query("SELECT * from responses WHERE qid = $qid") OR die(mysql_error());//corrected while ($ans = mysql_fetch_array($query2)) {?><?php echo $ans['response']; ?> (votes:<?php echo $ans['votes']; ?>)<br><?}?> as you can see it uses <?php echo $ans['votes']; ?> to get the amount of votes for the selected ID, now how do i add up the out-putted votes to get the total amount of votes for that question? And also, how do i turn that output into a %'age for each vote? I.E: yes (25 votes) (X%) no (15 votes) (X%) Total votes: 40 Thanks Rich Quote Link to comment https://forums.phpfreaks.com/topic/157389-help-doin-maths/ Share on other sites More sharing options...
premiso Posted May 8, 2009 Share Posted May 8, 2009 SELECT sum(votes) as votes, response from responses WHERE qid = $qid GROUP BY qid Unsure if my SQL is correct, but that should get you what you want. Quote Link to comment https://forums.phpfreaks.com/topic/157389-help-doin-maths/#findComment-829659 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 I think you should use COUNT(*). I don't know what data vote holds. Count should tell you how many rows there are, which should mean how many votes. Then use PHP to do the math. Quote Link to comment https://forums.phpfreaks.com/topic/157389-help-doin-maths/#findComment-829665 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.