SEARCH
Newsletter
Subscribe to get design tips, latest trends, free stuff and more.
It doesn't look like an e-mail address

hosting

  Web Programming PHP Rate it! Poll

Rate it! Poll

Joseph Skidmore PHP Aug 08, 2006

Rate it! PollThis tutorial will provide you with all the information needed to create your very own poll for your site. Using mysql to store the data and variables to store the questions it is easily updateable and very easy to edit.

This is the SQL query to use. If you don't know how to add this using phpmyadmin, refer to Weborum's MySQL tutorial.

SQL Query

CREATE TABLE 'RateTable' (
'ID' INT NOT NULL AUTO_INCREMENT ,
'rateID' INT NOT NULL ,
'IP' TEXT NOT NULL ,
'vote' TINYINT NOT NULL ,
'date' DATE NOT NULL ,
INDEX ( 'ID' )
);

Database Connection Details

<?php
$dbHost =; #Your db host
$dbUser =; #username
$dbPass =; #Pass
$dbname =; #Name of the database.
?>

I suggest you put this in an external file and include it inside the pages, but it's up to you. It'll work fine if you put it in the code, I find it annoying to retype though. Or if you decide to switch servers, you're going to have a lot of fun changing them all.

Hacker Protection

<?php
function dbinsans($text) {
$text = strip_tags ($text, "");
$text = str_replace(chr(10),"",$text);
$text = str_replace(chr(13), "<br>", $text);
$text = str_replace(""","*",$text);
$text = str_replace("'","*",$text);
$text = addslashes($text);
return($text);
}
?>

The hacker code goes above this next piece of code:

<?php
$ip = $_SERVER["REMOTE_ADDR"];
#logs user IP (used to check if they voted)


if (isset($_POST['submit']))
{
#if the user has clicked the submit button

$vote = dbinsans($_POST['select']);
#how the user voted, 0 - 10

$rateID = dbinsans($_POST['rateid']);
#which poll download they voted on! Allows multiple polls

#the next few pieces of code check the user IP against database at ID

$r=0;
#We're going to use the variable r as a switch to determine if they're in the database.

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
#connecting to the database area

mysql_select_db($dbname,$db);
#selecting our database

$requete = "SELECT IP FROM RateTable WHERE rateID='$rateID'";
#A query to pull IPs from the RateTable at the ID

$result = mysql_query ($requete,$db);
#Should return ips of voters

while ($article = mysql_fetch_object($result))
{
#scrolls through the ips
if ($article->IP == $ip)
{
$r=1;
#if they get here they're already in the system. Set r (our switch variable) to 1
}
}

if ($r==0)
{
#if r is 0 they were not found in the previous query
$sql = "INSERT INTO RateTable (rateID, IP, vote, date) VALUES ('$rateID', '$ip', '$vote', curdate())";
#Creates the insert query string.

mysql_query($sql, $db);
#Queries the database and adds the user.

}
}
?>

This code should go anywhere above the next section, in the head is fine for it even. Up to you.

<?php

$question = '<b>Poll quetion</b>';
$q1 = 'option 1';
$q2 = 'option 2';
$q3 = 'option 3';
$q4 = 'option 4';
$q5 = 'option 5';


$currentID= '1';
#Ok here is where it counts, the following is the code to display the select box.
#Let me tell you why currentID is important, let's say you grabbed this from
#A query of your file? Like a page that's listing it and describing it in detail
#You could set $currentID = to whatever variable you have for the download's unique ID
#AKA two tables and you'd be using the unique ID of the downloaded item and the
#ID here to connect them. It's up to you... You could just manually change this though.
#Just change 1 to whatever # of your downloads you want.


#check IP against database at ID code again

$r=0;
#r as a switch variable (I don't know if that's a recognized term, but I'm using it as a
#switch so I figured I'd call it that. Not to be confused with a switch statement.

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);

$requete = "SELECT IP FROM RateTable WHERE rateID='$currentID'";
#A query to pull IPs from the RateTable at the ID

$result = mysql_query ($requete,$db);
#Should return ips of voters

while ($article = mysql_fetch_object($result))
{
if ($article->IP == $ip)
{
$r=1;
#if you want this code in detail, look above it's the same as I used up there.
}
}
if ($r==0) #if there is no ip of our user in the database
{
if (!isset($_POST['submit']))
{
#if submit has not been pressed, notice the !

#the following is the form... if you know html most of it
#should be recognizable... the $_SERVER['SCRIPT_NAME'] is a global
#variable and returns whatever you name this file.

echo $question;
echo '<form method="post" action="'.$_SERVER['SCRIPT_NAME'].'">';
echo '<input name="rateid" value="'.$currentID.'" type="hidden" />';
echo '<input type="radio" name="select" value="1" />'.$q1.'<br />';
echo '<input type="radio" name="select" value="2" />'.$q2.'<br />';
echo '<input type="radio" name="select" value="3" />'.$q3.'<br />';
echo '<input type="radio" name="select" value="4" />'.$q4.'<br />';
echo '<input type="radio" name="select" value="5" />'.$q5.'<br />';
echo '<p><input class="submit" type="submit" name="submit" value="Submit" /></p>';
echo '</form>';
}
}
else
{
#already voted area if their ip is in the db then they see this.
$one = 0;
$two = 0;
$three = 0;
$four = 0;
$five = 0;
#Setting all these variables to 0...


$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT vote FROM RateTable WHERE rateID='$currentID'";
#A query to pull all the votes, but no other part, from the RateTable at the ID

$result = mysql_query ($requete,$db);


while ($article = mysql_fetch_object($result))
{
switch ($article->vote)
#the wonderful switch statement the vote should have 0-10
{
case 1: #if it equals 1
$one++;
break;
case 2: #and so on
$two++;
break;
case 3:
$three++;
break;
case 4:
$four++;
break;
case 5:
$five++;
break;
}
}
$total = $one + $two + $three + $four + $five;
#A little algebra.

#The round function works like this round($integer,amount after decimal to round to)...
#If you know html and css this shouldn't be too bad.
#The image I have growing by %... It will fill the box it's in.
#you can edit at your own will, have fun

if ($total==0) # if the total happens to be zero
$total=1; #this way it still shows the accurate votes but also will not cause a div by zero error.

echo '<p>'.$question.'</p>';
echo '<p>'.$q1.' - '.round((($one/$total)*100),2).'%<br /> <img src="/img_articles/9108/rating.gif" style="width:'.(round((($one/$total)*100),0)).'%; height:15px; border: 1px solid #9CC8F4;" alt="" /></p>';
echo '<p>'.$q2.' - '.round((($two/$total)*100),2).'%<br /> <img src="/img_articles/9108/rating.gif" style="width:'.(round((($two/$total)*100),0)).'%; height:15px; border: 1px solid #9CC8F4;" alt="" /></p>';
echo '<p>'.$q3.' - '.round((($three/$total)*100),2).'%<br /> <img src="/img_articles/9108/rating.gif" style="width:'.(round((($three/$total)*100),0)).'%; height:15px; border: 1px solid #9CC8F4;" alt="" /></p>';
echo '<p>'.$q4.' - '.round((($four/$total)*100),2).'%<br /> <img src="/img_articles/9108/rating.gif" style="width:'.(round((($four/$total)*100),0)).'%; height:15px; border: 1px solid #9CC8F4;" alt="" /></p>';
echo '<p>'.$q5.' - '.round((($five/$total)*100),2).'%<br /> <img src="/img_articles/9108/rating.gif" style="width:'.(round((($five/$total)*100),0)).'%; height:15px; border: 1px solid #9CC8F4;" alt="" /></p>';
echo '<p>'.$q6.' - '.round((($six/$total)*100),2).'%<br /> <img src="/img_articles/9108/rating.gif" style="width:'.(round((($six/$total)*100),0)).'%; height:15px; border: 1px solid #9CC8F4;" alt="" /></p>';
echo '<p>Number of Votes: '.$total.'</p>';
}
?>

Place this where you want your poll.

That is all the code you need to display and process the poll box. The image I made for the table is attached at the bottom of the page, but you can make your own, or if you know what your doing then you can make a separate image for each result.

Here is the rating.gif in zip format.

Code has been updated by Joseph Skidmore to make it easier to use.

subscribe to newsletter