Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 345
0 Users 5 Guests Online

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CREATE TABLE `poll` (
`id` int(11) NOT NULL auto_increment,
`question` varchar(40) NOT NULL default '',
`option1` varchar(30) NOT NULL default '',
`option2` varchar(30) NOT NULL default '',
`option3` varchar(30) NOT NULL default '',
`option4` varchar(30) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;

CREATE TABLE `pollvotes` (
`id` int(11) NOT NULL auto_increment,
`pollid` varchar(20) NOT NULL default '',
`vote` varchar(20) NOT NULL default '',
`username` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`)
) 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php 
ob_start
(); 
//allow cookies 
require("config.php"); 
// include config file 
if(!isset($_POST[create])){ 
//if submit button isnt pressed 
echo "<form method='post'> 
Poll Question:\ 
<input type='text' name='question' maxlength='40'>\ 
Option 1: 
<input type='text' name='1' maxlength='30'>\ 
Option 2: 
<input type='text' name='2' maxlength='30'>\ 
Option 3: 
<input type='text' name='3' maxlength='30'>\ 
Option 4: 
<input type='text' name='4' maxlength='30'>\ 
<input type='submit' name='create' value='Submit'></form>"

//echo html form 

if(
$_POST[create]){ 
//if submit button is pressed 
$question $_POST['question']; 
//get question field 
$op1 $_POST['1']; 
//get option 1 field 
$op2 $_POST['2']; 
//get option 2 field 
$op3 $_POST['3']; 
//get option 3 field 
$op4 $_POST['4']; 
//get option 4 field 
if($question == NULL || $op1 == NULL || $op2 == NULL || $op3 == NULL || $op4 == NULL){ 
// check if any fields are left blank 
echo "A field was left blank, please go back and fix this."
}else{ 
$addpoll mysql_query("INSERT INTO poll (question,option1,option2,option3,option4) VALUES 
('
$question','$op1','$op2','$op3','$op4')"); 
//insert values 
echo "The poll has been created."


?>


Step Four - poll.php

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php 
ob_start
(); 
//allow cookies and header changes 
require("config.php"); 
//include our config file 
if(!isset($_POST[vote_poll])){ 
//if the submit button isnt pushed 
$getcurrent mysql_query("SELECT * FROM poll ORDER BY id DESC LIMIT 1"); 
//get the current poll info 
while($r=mysql_fetch_array($getcurrent)){ 
$pollid $r['id']; 
if(!isset(
$_COOKIE['pollvote'])){ 
//if the cookie isnt set 
$voted "No"
}else{ 
if(
$_COOKIE['pollvote'] == $pollid){ 
$voted "Yes"
}else{ 
$voted "No"


//poll id 
$question $r['question']; 
//poll question 
$total mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid'"); 
//get poll votes 
$total mysql_num_rows($total); 
//get number of poll votes 
$option1 $r['option1']; 
//option 1 
$option2 $r['option2']; 
//option 2 
$option3 $r['option3']; 
//option 3 
$option4 $r['option4']; 
//option 4 
if($option1 == "na"){ 
//if option 1 doesnt exist 
}else{ 
$geta mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='1'"); 
$numa mysql_num_rows($geta); 
if(
$numa == 0){ 
$percenta "0%"
//bar percent = 0 
}else{ 
$percenta round(($numa $total) * 100)."%"


if(
$option2 == "na"){ 
//if option 2 doesnt exist 
}else{ 
$gets mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='2'"); 
$nums mysql_num_rows($gets); 
if(
$nums == 0){ 
$percents "0%"
//bar percent = 0 

}else{ 
$percents round(($nums $total) * 100)."%"


if(
$option3 == "na"){ 
//if option 3 doesnt exist 
}else{ 
$getd mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='3'"); 
$numd mysql_num_rows($getd); 
if(
$numd == 0){ 
$percentd "0%"
//bar percent = 0 
}else{ 
$percentd round(($numd $total) * 100)."%"


if(
$option4 == "na"){ 
//if option 4 doesnt exist 
}else{ 
$getf mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='4'"); 
$numf mysql_num_rows($getf); 
if(
$numf == 0){ 
$percentf "0%"
//bar percent = 0 
}else{ 
$percentf round(($numf $total) * 100)."%"


echo 
"<form method='post'> 
  <strong>
$question</strong>\ 
"

    if(
$voted == "Yes"){ 
    
//if they already voted 
    
echo "$option1($numa
<img src='poll.gif' height='10' 
width='
$percenta'>\";//echo option 1 
    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{ 
    //else 
    echo "
<input type='radio' name='vote' value='1'$option1

      <
input type='radio' name='vote' value='2'$option2

      <
input type='radio' name='vote' value='3'$option3

      <
input type='radio' name='vote' value='4'$option4
"; 
      //echo form to vote 
    } 
    echo "
<input type='submit' value='Submit' name='vote_poll'></form>"; 

}else{ 
$vote = $_POST['vote']; 
//get the vote 
$getcurrent = mysql_query("SELECT FROM poll ORDER BY id DESC LIMIT 1"); 
//get current poll 
while(
$r=mysql_fetch_array($getcurrent)){ 
$pollid = $r['id']; 
//poll id 

$voteonpoll = mysql_query("INSERT INTO pollvotes (pollid,vote,usernameVALUES 
('$pollid','$vote','$Uname')"); 
//vote 
echo "
Thank you for voting."; 
setcookie("
pollvote",$pollid,time()+360000000); 
//thanks 

?>


Thats all, it should be working fine.
Vic Vance
Author:
Views:
5009
Rating:
Posted on Friday 19th September 2008 at 08:59 PM
Dava
Dava
simple just remove some buttons
Posted on Thursday 18th September 2008 at 03:47 PM
Dalez
Dalez
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
Dono't matter, sorted it out.

There was a "" in the code.
Posted on Thursday 18th September 2008 at 03:43 PM
Dalez
Dalez
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
Line 93 needs a ;
Posted on Saturday 13th September 2008 at 12:01 PM
Dalez
Dalez
Mk, so no one answer ;l
Posted on Sunday 7th September 2008 at 08:34 AM
Dalez
Dalez
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
Vic Vance
Vic Vance
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
What if you want only TWO selectable answers? D:
Posted on Monday 1st September 2008 at 03:44 PM
Vic Vance
Vic Vance
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
TutorialNinja Image
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

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