Please Help

I’ve been trying to make this work for 4 days. I’ve decided I’m totally database inept. I have a table called “votes” in my database. It’s populated via a form where people vote “yes” or “no”. I need to get a count of all rows and a count of “yes” votes, then get an average i.e. 90% of respondents voted yes. I’m really new at both MySQL and PHP. Any help would be truly appreciate it. Heck, I’d even kiss you feet:)

SELECT VOTE, COUNT(*)FROM TABLE GROUP BY VOTE;

This should give you

+-------+------------+
| VOTE  | COUNT(*)   |
+-------+------------+
|   Y   |     5      |
|   N   |     8      |
+-------+------------+

And from there you can use the PHP to calculate the average

I guess I’m not just database inept. That seems to extend to PHP too. I’ve been looking at this and trying lots of things since I got your reply and though the query portion works great, I can’t for the life of me figure out the PHP part. When’s the foot kissing part scheduled?

First I would print out the results (check out print_r) to make sure they are what I/you think they are. If they are… there is a couple of things you can do from saving the results in variables and doing the math on the variables, to working with the original results and doing the math on them.

an example with my results:
[php]
$sql = 'SELECT VOTE, COUNT(*) AS cnt FROM TABLE GROUP BY VOTE; ';
$result = mysql_query($sql);
$array = array();
// cycle through the results
for($i=0;$i<2;$i++)
{
// get the results
$data = mysql_fetch_assoc($result);
// save the results in an array for later access
array_push($array, $data[‘cnt’]);
}
// the math part to find the percentage for yes votes
// percentage = yes_count divided by the sum of yes_count and no_count
$percent = $array[0]/($array[0]+$array[1]);
[/php]

This is off the top of my head so I’m sure there are errors but you get the idea.

Sponsor our Newsletter | Privacy Policy | Terms of Service