Rate it! Poll

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="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="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="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="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="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="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.


Related Articles

  • Basic Poll Script in PHP
    This article will teach you how to write a basic poll script in php. You will mysql and a new version of PHP.
  • Where to Get Some Scripts
    You may find the list of basic scripts here.
  • The Top 4 Ways to Profit From The Hidden Revenue In Your Blog
    Most webmasters and online business owners know that a blog can bring you additional revenue from advertising, and more traffic from search engines. Then there
  • Web 2.0 Concepts to Keep Your Members Coming Back
    Web 2.0 may be the most overused, and misunderstood, term of the decade. What it means, in a nutshell is providing a user driven website. Basically many membership marketing websites are Web 2.0, especially social networking sites.
  • Evaluating Guide for Website Performance
    Setting up a website is the very first step of an Internet marketing campaign, and the success or failure of your site depends greatly on how specifically you have defined your website goals.
  • Evaulating Website Performance
    Setting up a website is the very first step of an Internet marketing campaign, and the success or failure of your site depends greatly on how specifically you have defined your website goals. If you dont know what you want your site to accomplish, it will most likely fail to accomplish anything.
  • Make Your Forum Alive
    Is it worth adding a forum to your site? How can it become popular with targeted users? These are the question that almost every more or less experienced webmaster asks after getting some experience with the World Wide Web.
  • How To Get Profits From Your '404 Page Not Found' File?
    Page Not Found on this Server. Check the URL and try again. Or Refresh the page...
    This is the annoying error that we will come across every time we surf the web pages. Internet will be changing every minute and day. The web sites that were here are not found next day...
  • How To Improve Your Conversion Rate
    Hardly a day goes by that I dont get an email from someone saying: Michael, I just dont know what to do. We are not getting sales, nobody is signing up, we are not making any money. What is going wrong? They start thinking about......
  • Free Reports...Create A Massive Contact List...Overnight
    Free reports and ecourses are one of the fastest and best ways to build a profit making list. I have over built a list of over 5,000 subscribers in less than a year, all opt in, using ...

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.