Poll System


Step One - The database / sql
First we have to create our tables in the database,
open up phpMyAdmin or whatever softwear you use
to edit queries with.
And run the following query onto your database:

PHP Code
  1. CREATE TABLE `poll` (
  2. `id` int(11) NOT NULL auto_increment,
  3. `question` varchar(40) NOT NULL default '',
  4. `option1` varchar(30) NOT NULL default '',
  5. `option2` varchar(30) NOT NULL default '',
  6. `option3` varchar(30) NOT NULL default '',
  7. `option4` varchar(30) NOT NULL default '',
  8. PRIMARY KEY (`id`)
  9. ) TYPE=MyISAM AUTO_INCREMENT=3 ;
  10.  
  11. CREATE TABLE `pollvotes` (
  12. `id` int(11) NOT NULL auto_increment,
  13. `pollid` varchar(20) NOT NULL default '',
  14. `vote` varchar(20) NOT NULL default '',
  15. `username` varchar(50) NOT NULL default '',
  16. PRIMARY KEY (`id`)
  17. ) TYPE=MyISAM AUTO_INCREMENT=11 ;


Step Three - newpoll.php
Now lets create a new a file so we can actually make a poll.
Create a new file and name it newpoll.php and insert into it:

PHP Code
  1. <?php
  2. ob_start();
  3. //allow cookies
  4. require("config.php");
  5. // include config file
  6. if(!isset($_POST[create])){
  7. //if submit button isnt pressed
  8. echo "<form method='post'>
  9. Poll Question:\
  10. <input type='text' name='question' maxlength='40'>\
  11. Option 1:
  12. <input type='text' name='1' maxlength='30'>\
  13. Option 2:
  14. <input type='text' name='2' maxlength='30'>\
  15. Option 3:
  16. <input type='text' name='3' maxlength='30'>\
  17. Option 4:
  18. <input type='text' name='4' maxlength='30'>\
  19. <input type='submit' name='create' value='Submit'></form>";
  20. //echo html form
  21. }
  22. if($_POST[create]){
  23. //if submit button is pressed
  24. $question = $_POST['question'];
  25. //get question field
  26. $op1 = $_POST['1'];
  27. //get option 1 field
  28. $op2 = $_POST['2'];
  29. //get option 2 field
  30. $op3 = $_POST['3'];
  31. //get option 3 field
  32. $op4 = $_POST['4'];
  33. //get option 4 field
  34. if($question == NULL || $op1 == NULL || $op2 == NULL || $op3 == NULL || $op4 == NULL){
  35. // check if any fields are left blank
  36. echo "A field was left blank, please go back and fix this.";
  37. }else{
  38. $addpoll = mysql_query("INSERT INTO poll (question,option1,option2,option3,option4) VALUES
  39. ('$question','$op1','$op2','$op3','$op4')");
  40. //insert values
  41. echo "The poll has been created.";
  42. }
  43. }
  44. ?>


Step Four - poll.php

PHP Code
  1. <?php
  2. ob_start();
  3. //allow cookies and header changes
  4. require("config.php");
  5. //include our config file
  6. if(!isset($_POST[vote_poll])){
  7. //if the submit button isnt pushed
  8. $getcurrent = mysql_query("SELECT * FROM poll ORDER BY id DESC LIMIT 1");
  9. //get the current poll info
  10. while($r=mysql_fetch_array($getcurrent)){
  11. $pollid = $r['id'];
  12. if(!isset($_COOKIE['pollvote'])){
  13. //if the cookie isnt set
  14. $voted = "No";
  15. }else{
  16. if($_COOKIE['pollvote'] == $pollid){
  17. $voted = "Yes";
  18. }else{
  19. $voted = "No";
  20. }
  21. }
  22. //poll id
  23. $question = $r['question'];
  24. //poll question
  25. $total = mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid'");
  26. //get poll votes
  27. $total = mysql_num_rows($total);
  28. //get number of poll votes
  29. $option1 = $r['option1'];
  30. //option 1
  31. $option2 = $r['option2'];
  32. //option 2
  33. $option3 = $r['option3'];
  34. //option 3
  35. $option4 = $r['option4'];
  36. //option 4
  37. if($option1 == "na"){
  38. //if option 1 doesnt exist
  39. }else{
  40. $geta = mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='1'");
  41. $numa = mysql_num_rows($geta);
  42. if($numa == 0){
  43. $percenta = "0%";
  44. //bar percent = 0
  45. }else{
  46. $percenta = round(($numa / $total) * 100)."%";
  47. }
  48. }
  49. if($option2 == "na"){
  50. //if option 2 doesnt exist
  51. }else{
  52. $gets = mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='2'");
  53. $nums = mysql_num_rows($gets);
  54. if($nums == 0){
  55. $percents = "0%";
  56. //bar percent = 0
  57.  
  58. }else{
  59. $percents = round(($nums / $total) * 100)."%";
  60. }
  61. }
  62. if($option3 == "na"){
  63. //if option 3 doesnt exist
  64. }else{
  65. $getd = mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='3'");
  66. $numd = mysql_num_rows($getd);
  67. if($numd == 0){
  68. $percentd = "0%";
  69. //bar percent = 0
  70. }else{
  71. $percentd = round(($numd / $total) * 100)."%";
  72. }
  73. }
  74. if($option4 == "na"){
  75. //if option 4 doesnt exist
  76. }else{
  77. $getf = mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='4'");
  78. $numf = mysql_num_rows($getf);
  79. if($numf == 0){
  80. $percentf = "0%";
  81. //bar percent = 0
  82. }else{
  83. $percentf = round(($numf / $total) * 100)."%";
  84. }
  85. }
  86. echo "<form method='post'>
  87. <strong>$question</strong>\
  88. ";
  89. if($voted == "Yes"){
  90. //if they already voted
  91. echo "$option1($numa)
  92. <img src='poll.gif' height='10'
  93. width='$percenta'>\";//echo option 1
  94. echo "$option2($nums)
  95. <img src='poll.gif' height='10'
  96. width='$percents'>\";//echo option 2
  97. echo "$option3($numd)
  98. <img src='poll.gif' height='10'
  99. width='$percentd'>\";//echo option 3
  100. echo "$option4($numf)
  101. <img src='poll.gif' height='10'
  102. width='$percentf'>\";//echo option 4
  103. }else{
  104. //else
  105. echo "<input type='radio' name='vote' value='1'> $option1\
  106.  
  107. <input type='radio' name='vote' value='2'> $option2\
  108.  
  109. <input type='radio' name='vote' value='3'> $option3\
  110.  
  111. <input type='radio' name='vote' value='4'> $option4\
  112. ";
  113. //echo form to vote
  114. }
  115. echo "<input type='submit' value='Submit' name='vote_poll'></form>";
  116. }
  117. }else{
  118. $vote = $_POST['vote'];
  119. //get the vote
  120. $getcurrent = mysql_query("SELECT * FROM poll ORDER BY id DESC LIMIT 1");
  121. //get current poll
  122. while($r=mysql_fetch_array($getcurrent)){
  123. $pollid = $r['id'];
  124. //poll id
  125. }
  126. $voteonpoll = mysql_query("INSERT INTO pollvotes (pollid,vote,username) VALUES
  127. ('$pollid','$vote','$Uname')");
  128. //vote
  129. echo "Thank you for voting.";
  130. setcookie("pollvote",$pollid,time()+360000000);
  131. //thanks
  132. }
  133. ?>


Thats all, it should be working fine.
VicVance's Avatar
Author:
Views:
6,556
Rating:
Posted on Friday 19th September 2008 at 08:59 PM
Dava
Dava's Avatar
simple just remove some buttons
Posted on Thursday 18th September 2008 at 03:47 PM
Dalez
Dalez's Avatar
How would i make it so i can add as many answers as i like?

e.g only 2 answers?
Posted on Thursday 18th September 2008 at 03:44 PM
Dalez
Dalez's Avatar
Dono't matter, sorted it out.

There was a "" in the code.
Posted on Thursday 18th September 2008 at 03:43 PM
Dalez
Dalez's Avatar
Already has one:

Code
if($voted == "Yes"){
//if they already voted
echo "$option1($numa)
<img src='poll.gif' height='10'
width='$percenta'>";
echo "$option2($nums)
<img src='poll.gif' height='10'
width='$percents'>";//echo option 2
echo "$option3($numd)
<img src='poll.gif' height='10'
width='$percentd'>";//echo option 3
echo "$option4($numf)
<img src='poll.gif' height='10'
width='$percentf'>";//echo option 4
}else{
Posted on Saturday 13th September 2008 at 12:23 PM
ShadowMage
ShadowMage's Avatar
Line 93 needs a ;
Posted on Saturday 13th September 2008 at 12:01 PM
Dalez
Dalez's Avatar
Mk, so no one answer ;l
Posted on Sunday 7th September 2008 at 08:34 AM
Dalez
Dalez's Avatar
Code
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:xampphtdocsv2poll.php on line 94


And its in a echo, so i don't know where to put it..
Posted on Monday 1st September 2008 at 06:39 PM
VicVance
VicVance's Avatar
Leve the fild blank. If that does not work just edit the IF statement.
Posted on Monday 1st September 2008 at 04:31 PM
UrbanTwitch
UrbanTwitch's Avatar
What if you want only TWO selectable answers? D:
Posted on Monday 1st September 2008 at 03:44 PM
VicVance
VicVance's Avatar
I have tried it too it works fine. but see there no delete or edit once you add the poll it gets added then when u add it again it replaces the question. Here are my images of poll when I tried it out

TutorialNinja Image

Some features are lacking but I am thinking we came add new features in as we comment on the tutorail. but the main thing is it works.

And Shadow move my ffilate tutorial to user syste please.

AFFILIATE TUTORAIL

https://rmb-scripting.com/tutorials.php?tutorial&tid=349