Does anyone know a code to make a poll on a page with as many options as l want? l have to be able to coustomise all colors, and l would like to be able to store it in a seperate document and link to it so it doesn't increase load time for users. And it must only allow one vote per user, and it would be nice if l could set a value for each option already (l am transfering from a previous server and they hosted the poll so l can't take it with me)
Creating a poll
Alright the template can be changed any time but I will do the main scripting part for you.
Here Is A PHP Poll.
Open Up phpMyAdmin and run the following code
Name this file config.php
Call this file newpoll.php
Call this file poll.php
With that use tables for each radio box / option.
That shouldwork.
Here Is A PHP Poll.
Open Up phpMyAdmin and run the following code
| Quote: |
| 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 ; |
Name this file config.php
| Quote: |
| <?php
$connection = mysql_connect("HOST", "USERNAME", "PASSWORD"); mysql_select_db("DB NAME", $connection); $date = date("l, F jS, Y"); ?> |
Call this file newpoll.php
| Quote: |
| <?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:\<br /> <input type='text' name='question' maxlength='40'>\<br /> Option 1:<br /> <input type='text' name='1' maxlength='30'>\<br /> Option 2:<br /> <input type='text' name='2' maxlength='30'>\<br /> Option 3:<br /> <input type='text' name='3' maxlength='30'>\<br /> Option 4:<br /> <input type='text' name='4' maxlength='30'>\<br /> <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."; } } ?> |
Call this file poll.php
| Quote: |
| <?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>\<br /> "; if($voted == "Yes"){ //if they already voted echo "$option1($numa) <img src='poll.gif' height='10' width='$percenta'>\<br />";//echo option 1 echo "$option2($nums) <img src='poll.gif' height='10' width='$percents'>\<br />";//echo option 2 echo "$option3($numd) <img src='poll.gif' height='10' width='$percentd'>\<br />";//echo option 3 echo "$option4($numf) <img src='poll.gif' height='10' width='$percentf'>\<br />";//echo option 4 }else{ //else echo "<input type='radio' name='vote' value='1'> $option1\<br /> <input type='radio' name='vote' value='2'> $option2\<br /> <input type='radio' name='vote' value='3'> $option3\<br /> <input type='radio' name='vote' value='4'> $option4\<br /> "; //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,username) VALUES ('$pollid','$vote','$Uname')"); //vote echo "Thank you for voting."; setcookie("pollvote",$pollid,time()+360000000); //thanks } ?> |
With that use tables for each radio box / option.
That shouldwork.
hey thanks for that
