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: 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
/
|
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
}
?> |
|