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 using radio button. The could be used for any number of items you want to rate without changing a bit.

This polling/rating example uses radio buttons. It uses the same pollresult.php as in lessonPolling/Rating System in PHP. The vote.php is changed slightly since we don't want to prompt user.

This example won't explain code very detail like the lesson Polling/Rating System in PHP.

You may use it to do opinion poll, rate your software,images, news, music,anything.

Here is the modified example:

Vote for this tutorial:
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home1/chinesel/public_html/kidslovepc/php-tutorial/php-poll-rating-radio.php on line 75

Warning: include(http://www.kidslovepc.com/php-tutorial/pollresult.php?id=2): failed to open stream: no suitable wrapper could be found in /home1/chinesel/public_html/kidslovepc/php-tutorial/php-poll-rating-radio.php on line 75

Warning: include(): Failed opening 'http://www.kidslovepc.com/php-tutorial/pollresult.php?id=2' for inclusion (include_path='.:/usr/php/56/usr/share/pear') in /home1/chinesel/public_html/kidslovepc/php-tutorial/php-poll-rating-radio.php on line 75

Poor Good

Using session variable to control voting frequency

Suppose you want to rate an tutorial,which itemid is 24. You need an form like the above one:

<?php session_start()?>
<html>
<body>
<?php virtual("vote.php") ?>
Vote for this tutorial: <strong>
<?php include 'http://www.kidslovepc.com/php-tutorial/pollresult.php?id=24'; ?>
</strong>
<form action="" method="post">
Poor
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input name="select" type="radio" value="5" checked>
Good
<input type="submit" name="vote" value="Vote">
<input name="itemid" type="hidden" id="itemid" value="24">
</form>
</body>
</html>

Please note that:


  • <?php session_start()?>
    must be put in the first line, no empty space before it.
  • <?php include 'http://www.kidslovepc.com/php-tutorial/pollresult.php?id=24'; ?> is pulling out voting result.

For multiple items in one page, you only need include vote.php for once on the top. But you need call <?php include 'http://www.kidslovepc.com/php-tutorial/pollresult.php?id=XXX'; ?> for different itemid XXX.

We use session variable to control voting frequency since we don't like one user vote it again and again.

We set session variable for each voting item. Then we check it in PHP code. If the session variable set, ignore the vote. Otherwise save it to database. For simplicity, the session variable for item 24 could be "vote24", for item 2 is "vote2" and so on.

Here is the code of vote.php for this example.

<?php

if(isset($_POST['vote']))
{
//get voting value;

$votevalue=$_POST['select'];
$id=$_POST['itemid'];

//check session
if( !isset($_SESSION["vote$id"])) //if session variable is not set
{
$_SESSION["vote$id"]=1;

//===============================
//saving vote data to database
//===============================

//open database connection
$username = "yourname";
$password = "yourpassword"; //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>");

//retrieve old voting result

$query="select votes,result from polling where itemid=$id";
$result=mysql_query($query) or die('Error, query failed');

$oldVote=mysql_result($result,0,"votes");
$oldResult=mysql_result($result,0,"result");

//calculate new result
$newVote=$oldVote+1;
$newResult=($oldVote*$oldResult+$votevalue)/$newVote;

//saving new result to database
$query="update polling set votes=$newVote,result=$newResult where itemid=$id";
mysql_query($query) or die('Error, query failed');
mysql_close($link);

} //end of checking session
}
?>

 

Reference: Polling/Rating System in PHP
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