Re: percentage
| From: | Jake Malone | Date: | Thu, 24 Apr 2003 03:48:46 +0000 |
| Subject: | Re: percentage | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-145114@lists.php.net to get a copy of this message | ||
one thing i learned about using php with mysql, is that you can actually
save yourself a lot of time by simply using mysql's built in functionality
to do these kinds of tasks for you.
i don't know if this is exactly right, but i would imagine you could do
something like this:
first do a query to get the total number of answers: SELECT COUNT(Answer_pl)
FROM pollTable. return this is as a php variable, like $totalAnswers
then do a query which calculates what percent answer x is out of the total.
like this: SELECT ROUND($totalAnswers/COUNT(Answer_pl)) AS percentage FROM
pollTable WHERE Answer_pl = '4'
i think this should work, but i haven't really tested it. you might even be
able to do it all with one query, but i'm not sure what it would be. try
checking out the mysql manual....look for math functions and arithmetic
operations: http://www.mysql.com/doc/en/Mathematical_functions.html
if you want to use strictly php to handle the calculation, it's pretty much
the same process. first get the total number of answers ($totalAnswers).
then get total number of answers that equal, say, '4'.
$percentage = round($totalAnswers/$aSpecificAnswer);
the functions are virtually the same. just a matter of which way you prefer
to do it.
-jake
"Tbo" <tim@copiousdg.com> wrote in message
news:20030424024340.65501.qmail@pb1.pair.com...
> I have an extreeeeemely basic poll with the results saved in a mysql db.
> What is the easiest way to view percentages of each question answered on a
> php page? The poll table in the db has 1 answer field(Answer_pl) with
> answers ranging from 1-4. The questions are static so I only have a
answer
> table in the db. There is also an auto-id for each answer(ID_pl). Any
> thoughts or tutorial suggestions?
>
> Thanks.
>
> --
> Tim Haskins
>
>
>
>