Sponsor
Now Playing
- Alix Perez – I'm Free 3 hours ago
- Alix Perez – Intersections 3 hours ago
- Alix Perez – Forsaken 3 hours ago
- Glen E Ston – Ouroboros (Original Mix) 4 hours ago
- Black Sun Empire – Everything 4 hours ago
Tutorial: How to build a poll system with PHP and mySQL
April 11, 2008,
48,309 views
First up we have:
$total = mysql_query("SELECT `questions`.`pid` FROM `responses`, `questions` WHERE `responses`.`qid`=`questions`.`id` AND pid='".$poll['id']."'");
$total = mysql_num_rows($total);
This selects all the responses from the selected poll. It then puts it in a variable which we can use to calculate the number of rows from. This calculation is important as it allows us to firstly show the total number of vote, and secondly allows us to calculate the bar lengths used to graphically depict the results.
The next part is HTML, you don’t need to worry about this too much, just opening up a table and showing the title of the poll.
This gets each percentage and sets the width of the table cells, the left one containing an image which repeats it self over the width of the table cell, aka a background! This line here: 0){?>style="background: url('images/bar.jpg') repeat-x;"
Checks if the percentage equals 0. If it does, don’t worry about displaying a background image. All you will see then is just a 1pixel image, which is not theoretically correct.
We then show the number of votes for that question and then leave the questions cycle. Lastly we display the total number of votes (remember how we calculated $total above) with:
Then we close all if statements and finish the page. Next i will teach you how to add questions and make new polls via the direct editing of the database. | ||||||
70 Comments
voidet. Of course! Sent you some cash and put a link in.
Many thanks for your hard work!
Hey!
Thanks alot for the donation!
I really appreciate that! If people are using the tutorial as a learning tool i’m all for it, we both can build something up that way. But if people are going to use it for their own web apps, it’s not right to just take and not give anything back
But thanks alot for the donation! I am glad it worked out for you
Also if you need any future help with anything just let me know!
Honestly it was a learning tool for me. And I ended up using it successfully which I thought was very cool.
I am happy to give back and may call on you again. Thanks very much.
Hey Artech!
Really cool portfolio you have!
Love the graphics!
Thanks Voidet.
I enjoy working with flash. Creating with graphics in mind is usually how I approach things. But I’m getting more and more into the scripting side of things. Really appreciating the functionality and management.
You should get into the frameworks!
Just jump into CakePHP
will this code work if i want to ask a series of independant yes or no question that i want to rate separately?
This is a poll script.
To turn it into a survey type program would require some refactoring.
Not too much if you want it hardcoded, but alot if you want it to be dynamic
Hi, great tutorial, thanks man.
hi, anyone know how we get the value of $responses['total'] hmm i starter in php wondering where we get the ‘total’ from? thx for any help..
IZ please clarify here!
Do an echo $responses['total'] to output the total number of responses!
Really good tutorial!! THX!! I’m just modifying a bit to fit on my page and to my taste.
But again, thx, i learned a lot here!
Hi.. nice tutoria! i have a question
Warning: Wrong parameter count for max() in C:\wamp\www\poll\poll.php on line 17
what does it mean? and how can i solve it? thanks…
Hey Ralph,
Below “$query = mysql_query(“SELECT COUNT(`id`) as hits FROM `responses` GROUP BY `qid`”);”
Add:
$me = array();
thanks VoiDeT.. ill try it! ^__^
Hi, followed your tutorial, this is for personal learning.. im new to PHP. was a great tutorial even for those who don’t code all the time. I just had 2 questions, everything is up and working, but my main page is also called index.php, if i change the name of the index.php that you set as default would it still work fine?
also, how do i add more polls? i want to add atleast three or four questions, thanks.
Try changing the name and see what happens.
There might be references to index.php in the code, or it could just be SELF. I forget a lot of the code in this, as this desperately needs a re-write.
If you want to add more polls or questions you should do so in the database. However you will need to cycle through the polls somehow.
Thanks,
Richard
Thanks, I will try changing the name and see what happens, I basically want to have 3-4 polls running at the same time, if I cant sort it out I will have to stick to one poll
Hi Voidet,
I clicked on the link you give above but it seems to be malfunctioning…http://www.jotlab.com/tutorials/poll/index.php
++++++++++++++++++++++++++++++++++++++++++++++++Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘voidet_jotlab’@'localhost’ (using password: YES) in /var/www/jotlab.com/public_html/tutorials/poll/config.php on line 10
Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user ‘www-data’@'localhost’ (using password: NO) in /var/www/jotlab.com/public_html/tutorials/poll/config.php on line 11
Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /var/www/jotlab.com/public_html/tutorials/poll/config.php on line 11
Warning: mysql_query() [function.mysql-query]: Access denied for user ‘www-data’@'localhost’ (using password: NO) in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 3
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 3
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 4
Warning: mysql_query() [function.mysql-query]: Access denied for user ‘www-data’@'localhost’ (using password: NO) in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 13
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 13
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 14
Warning: Wrong parameter count for max() in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 17
Warning: mysql_query() [function.mysql-query]: Access denied for user ‘www-data’@'localhost’ (using password: NO) in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 19
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 19
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 21
Warning: mysql_query() [function.mysql-query]: Access denied for user ‘www-data’@'localhost’ (using password: NO) in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 77
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 77
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 78
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
any help is always appreciated
rca08207 at bigpond dot net dot au
A very detailed and helpful tutorial indeed. Loved it. I made a collection of all such tutorials and scripts (including this one) at 11 PHP Voting Scripts and Tutorials. I hope it would be helpful to many.
3 Trackbacks
[...] 7.How to build a poll system with PHP and mySQL : [...]
[...] Read the original post: Tutorial: How to build a poll system with PHP and mySQL • Jotlab [...]
[...] Source: http://www.jotlab.com/2008/04/11/tutorial-how-to-build-a-poll-system-in-php-and-mysql/ [...]