KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

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_connect(): Access denied for user 'chinesel_hsun'@'localhost' (using password: YES) in /home1/chinesel/public_html/kidslovepc/php-tutorial/pollresult.php on line 15
Unable to specified database

This tutorial includes 3 sections:

  1. Create database : how to save polling/rating results in MySQL
  2. 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.
  3. 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

Icon
Name
0.gif
0half.gif
1.gif
1half.gif
2.gif
2half.gif
3.gif
3half.gif
4.gif
4half.gif
5.gif

Here are some other polling icons:

Icon

 

 

 

Back: dealing with poll/rate form
PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
Set cookie and delete cookie
Most often used MySQL functions

 

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer