|
This tutorial
will show you how to create a two option poll system using php/mysql. The
options will be either yes or no. It will allow all users to vote, but
will log their ip and not allow them to vote more than once. Instead, it
will show the current standings in comparison with the others with images.
First we'll need to create the database:
sql:
CREATE TABLE polls {
id INT NOT NULL AUTO_INCREMENT;
title VARCHAR(30);
question VARCHAR(100);
no INT;
yes INT;
votes INT;
PRIMARY KEY(id);
};
CREATE TABLE votes {
vote_id INT NOT NULL
AUTO_INCREMENT;
ip VARCHAR(15);
PRIMARY KEY(vote_id);
}
Our tables
are created. The polls table will store the poll info, including the amount
of votes for each option and the total votes for that poll. It will also
store the title and the question of the poll. The votes table will store
the ip of the voter so they cannot vote more than once. So there are basically
three steps to the script we need to set up. Display the title, question,
and options, if user has already voted display results. Validate and process
vote. Show the results of the poll.
Ok so first
we need to get the user's ip address.
php:
$remoteIP =
$_SERVER['REMOTE_ADDR'];
if(strstr($remoteIP,
', '))
{
$ips =
explode(',
', $remoteIP);
$remoteIP =
$ips[0];
}
Once in awhile,
$_SERVER['REMOTE_ADDR'] will return more than one ip seperated by a comma.
So it is necessary to check for this and take the first option if it happens.
Now we will check to see if the user has already voted:
php:
<?php $sql = "SELECT ip FROM votes"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc()) { if($row['ip'] == $remoteIP) { $smarty->display("result.tpl"); exit; } }
?>
Ok so now
we've checked to see if the user has already voted and forwarded them
to the results if they have. (We'll do the result.tpl page later on.)
Next we'll create the script to display the poll, we'll need to select
the title and questions from the database:
php:
$sql = "SELECT
title, question, votes, yes, no FROM polls";
$result = mysql_query($sql)
or die(mysql_error());
$row = mysql_fetch_assoc($result);
$smarty->assign("info",
$row);
$smarty->display("poll.tpl");
Ok now the poll.tpl
page:
html:
<html>
<head>
<title></title>
</head>
<body>
<b>{$info.title}</b><br>
{$info.question}<br>
<form action="{$smarty.server.PHP_SELF}"
method="post">
Yes:
<input type="radio"
name="option"
value=1
checked><br>
No:
<input type="radio"
name="option"
value=0><br>
<input type="submit"
value="Submit"><br>
</form>
</body>
</html>
?>
Now that displaying
the poll is done, we'll create some code to process this form. Pretty much
all we'll need to do is update the database since there is no need to check
for wrong input since our only input is a radio button.
php:
if($_POST)
// check for a form submit
{ if($_POST['option'] == 1) // check to see if yes was selected { //add one to the yes column if it is $sql = "UPDATE polls SET yes = yes+1, votes = votes+1"; mysql_query($sql) or die(mysql_error()); //insert their ip so they cannot vote more than once $sql = "INSERT INTO votes(ip) VALUES('$remoteIP')"; mysql_query($sql) or die(mysql_error()); } else { //add one to the no column if it isnt $sql = "UPDATE polls SET no = no+1, votes = votes+1"; mysql_query($sql) or die(mysql_error()); //insert their ip so they cannot vote more than once $sql = "INSERT INTO votes(ip) VALUES('$remoteIP')"; mysql_query($sql) or die(mysql_error()); }
}
Ok now we
will need to display the results. To do so we will want to display bars
of a certain length, something like this:
Ok first we
will do the php for this page:
php:
$sql = "SELECT
title, question, votes, yes, no FROM polls";
$result = mysql_query($sql)
or die(mysql_error());
$row = mysql_fetch_assoc($result);
$smarty->assign("info",
$row);
Now the html:
html:
<html>
<head> <title></title> </head> <body> <b>{$info.title}</b><br> {$info.question}<br> Yes<br>
<img src="red.jpg"
width="{$info.yes}">
No<br>
<img src="red.jpg"
width="{$info.no}">
<div align="right">{$info.votes}</div>
</form> </body>
</html>
red.jpg should
be an image you create to show the results of the poll. Ok now we will build
one php page out of this:
php:
<?php $sql = "SELECT title, question, votes, yes, no FROM polls"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($result); $smarty->assign("info" $row);
$remoteIP = $_SERVER['REMOTE_ADDR']; if(strstr($remoteIP, ', ')) { $ips = explode(', ', $remoteIP); $remoteIP = $ips[0]; }
$sql = "SELECT ip FROM votes"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc()) { if($row['ip'] == $remoteIP) { $smarty->display("result.tpl"); exit; } }
if($_POST) // check for a form submit { if($_POST['option'] == 1) // check to see if yes was selected { //add one to the yes column if it is $sql = "UPDATE polls SET yes = yes+1, votes = votes+1"; mysql_query($sql) or die(mysql_error()); //insert their ip so they cannot vote more than once $sql = "INSERT INTO votes(ip) VALUES('$remoteIP')"; mysql_query($sql) or die(mysql_error()); } else { //add one to the no column if it isnt $sql = "UPDATE polls SET no = no+1, votes = votes+1"; mysql_query($sql) or die(mysql_error()); //insert their ip so they cannot vote more than once $sql = "INSERT INTO votes(ip) VALUES('$remoteIP')"; mysql_query($sql) or die(mysql_error()); } $smarty->display("result.tpl"); exit;
}
$smarty->display("poll.tpl"); ?>
Thats all,
you should now have a working poll. Lastly you'll need to build a page
to add a poll to the database. It would basically just need to have a
form and a few insert statements. Have fun :)
|