creativz
creativz

Reputation: 10787

Php/Mysql Simple survey

I want to make a little php poll. The script should ask the users a question they can only answer by numbers from 0-999. After pressing the submit button the data should be stored into mysql. So I just want to know how much users choosed the same number (in percent). It's a simple poll but I don't want any output to be shown.

Upvotes: 2

Views: 2788

Answers (2)

confiq
confiq

Reputation: 2918

here you go: http://code.tutsplus.com/articles/creating-a-web-poll-with-php--net-14257 It's simple tutorial how to make poll.

YOu can use http://www.phpkobo.com/ajax_poll.php if you need something done..

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838106

You need to use COUNT and GROUP BY:

SELECT
    number,
    COUNT(number) * 100 / (SELECT COUNT(*) FROM table1) AS percent
FROM table1
GROUP BY number
ORDER BY COUNT(number) DESC

Results:

number  percent
2       50.0000
3       30.0000
1       20.0000

Test data:

CREATE TABLE table1 (number INT NOT NULL);
INSERT INTO table1 (number) VALUES (1),(1),(2),(2),(2),(2),(2),(3),(3),(3);

Upvotes: 4

Related Questions