A Poll/Rating
system in PHP and MySQL
Before you start this lesson, please read the following
lessons for your reference:
This lesson will show you how to create a simple poll/rating
system in PHP and MySQL. The could be used for any number of items
you want to rate without changing a bit.
You may use it to do opinion poll, rate your software,images,
news, music,anything.
Here is the example:
|
Vote for this tutorial
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 2 in /home/chinesel/public_html/kidslovepc/php-tutorial/pollresult.php on line 22
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 2 in /home/chinesel/public_html/kidslovepc/php-tutorial/pollresult.php on line 23
/
|
This tutorial includes 3 sections:
- Create database : how to save polling/rating results in MySQL
- Dealing poll/rate Form:
how to saving polling/rating result to database and how to set
cookie to prevent client repeat voting on the same item.
- Retrieve poll/rate result:
retrieve poll/rate result for item and assign corresponding poll/rate
icon to it.
Retrieve poll/rate result from MySQL database
Usually we need an icon to indicate a polling/rating result, such
as like this:  
These icons should respond to the polling result in database. The
following code will assign an proper icon to a item according to its
polling result in database.
Assume we need put an icon besides item 1 and polling
result like result/votes(for example 3.2/46) by <?php
include("pollresult.php?id=1")?>. Note
that this including code should be used in .php file. If you use other
HTML file, please use corresponding server side include.
Here is pollresult.php code:
| <?php
//get item id
$id=$_GET['id'];
//===============================
//get polling result from database
//===============================
//open database connection
$username = "yourname";
$password = "yourpass"; //input your password here.
$database = "yourdb";
//connect to database
$link = mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");
//retrievevoting result
$query = "select votes,result from polling where itemid=$id";
$result = mysql_query($query) or die('Error, query failed');
$votes = mysql_result($result,0,"votes");
$voteResult = mysql_result($result,0,"result");
//assign icons
$digit1 = substr( $voteResult,0,1); //get first digit;
$digit2 = substr( $voteResult,2,1); //the last digit;
$half="";
if (strcmp($digit2,"0") == 0 )
$half ="";
else
$half = "half";
echo "<img src=\"images/$digit1$half.gif\"> $voteResult/$votes";
mysql_close($link);
?> |
pollresult.php pulls voting result from database and display proper
icon based on polling results.
$voteResult = mysql_result($result,0,"result"); gives the
polling result in database.
$digit1 is the first digit of polling result.
$digti2 is the last digit of polling result. For example, if polling
result is 2.4, $digit1 is 2, $digit2 is 4.
if $digit2 is not 0, will assign an icon with "half"
in its name, such as 2half.gif
The following icons are used by this example
Here are some other polling icons:
|