I've found a great piece of code in a php coding book and I'm trying make it do the following:
Since I plan to have a good number of polls, I've got this handy $poll variable. And I've decided to use cookies to only allow visiters to vote once per poll. As you can see in the script bellow, I've got the Setting of the cookie down, but beyond that I can't get anything to work. First is my test code, bellow:
<?PHP
$NumberOfPolls = 15.
if cookie has not been set before
$poll = 1.
if cookie has been set before
$poll = whatever number the cookie says plus one.
if $poll = more than $NumberOfPoll
print "I'm sorry, but you have already voted on all of the current Polls. Please come back soon!" and do NOT do the rest.
?> End PHP
As far as how to impliment that, it baffles me. The acutual code from the book is bellow. Like I said the TEST code above would be used to set what $poll equals and if you're allowed to vote.
<?php
//set the poll selection for this script
$poll=1;
// Check for Post variables and transfer them to a script variable.
if (isset($HTTP_POST_VARS[vote]) )
$vote = $HTTP_POST_VARS[vote];
else
$vote=NULL;
// Connect to MySql and select the proper database
$DB_SERVER = "XXXXXX";
$DB_USER = "XXXXX";
$DB_PASS = "XXXXX";
$DB_NAME = "vpoll";
$DB_TABLE = "data";
$link = mysql_pconnect($DB_SERVER, $DB_USER, $DB_PASS);
mysql_select_db($DB_NAME);
$result = mysql_query("SELECT * FROM $DB_TABLE WHERE tid=1");
// If a vote has been registered update the count in the database
if ($vote) {
$qid = mysql_query("UPDATE data SET votes=votes+1 WHERE name='$vote' and tid='$poll'");
//<<<<<--------------Set Cookie------------->>>>>>
setcookie("poll", $poll);
//<<<<-------------End Set Cookie----------->>>>>>
if ( ! $qid )
die ( "getPoll fatal error: ".mysql_error() );
}
?>
<html>
<head>
<title>Poll</title>
<style type=text/css>
body { margin: 10px; background-color:#FFFFFF; font-family: Verdana; }
table { background-color:#E2E2E2; }
td.background { background-color: #000000; border:0px; }
td.barcolor { background-color: #FFFF00; border:0px; }
table.barcolor { background-color: #FFFF00; border:0px; }
td.text { font-size:8pt; }
td.question { font-size:10pt; color:#FFFFFF; font-weight:bold; text-align:center; background-color: #000000;}
</style>
</head><body>
<?php
$qid=mysql_db_query("vpoll","select * from topic where tid=$poll");
if ( ! $qid )
die ( "getPoll fatal error: ".mysql_error() );
else{
$text=mysql_fetch_row($qid);
mysql_free_result($qid);
}
$qid=mysql_db_query("vpoll","SELECT sum(votes) as sum FROM data WHERE tid=$poll");
if ( ! $qid )
die ( "getPoll fatal error: ".mysql_error() );
else{
$sum = mysql_result($qid,0,"sum");
mysql_free_result($qid);
}
$qid=mysql_db_query("vpoll","select * from data where tid=$poll order by votes DESC") or die("Database Query Error");;
if($vote){
print "<table border=0 width=150 class=\"background\"><tr><td class=\"question\" colspan=\"3\">$text[1]<br><br></td></tr><tr><td class=\"text\">$text[2]</td><td class=\"text\">Results</td><td class=\"text\">%</td></tr>\n";
while($row=mysql_fetch_row($qid)) {
print "<tr><td align=center></td>";
print "<td class=\"text\" colspan=\"2\"></td></tr><tr>
<td class=\"text\" align=\"left\">".$row[1]."</td><td class=\"text\">";
if($sum && $row[2]) {
$per = (int)(100 * $row[2]/$sum);
print "<table align=center border=0 cellspacing=0 cellpadding=1 width=\"80%\" height=\"10\">\n";
print " <tr>\n";
print " <td class=\"background\">\n";
print " <table align=left border=0 cellspacing=0 cellpadding=0 width=\"$per%\" height=\"100%\">\n";
print " <tr>\n";
print " <td class=\"barcolor\">\n";
print " <table class=\"barcolor\"><tr><td></td></tr></table>\n";
print " </td>\n";
print " </tr>\n";
print " </table>\n";
print " </td>\n";
print " </tr>\n";
print "</table>\n";
print"</td><td class=\"text\">$per</td>";
}
print "</tr>\n";
}
print "<tr><td class=\"text\" colspan=\"3\" align=\"center\"></td></tr>";
print "</table>\n";
}
else{
print "<form action=\"$PHP_SELF\" method=\"POST\">";
print "<table border=0 width=150><tr><td class=\"question\" colspan=\"3\">$text[1]<br><br></td></tr><tr><td class=\"text\" align=\"center\">Select</td><td class=\"text\">$text[2]</td><td class=\"text\"></td></tr>\n";
while($row=mysql_fetch_row($qid)) {
print "<tr><td align=center><input type=radio name=vote value=\"$row[1]\"></td>";
print "<td class=\"text\" colspan=\"2\">" .$row[1]."</td></tr><tr>
<td class=\"text\" align=\"center\"></td><td class=\"text\">";
if($sum && (int)$row[2]) {
$per = (int)(100 * $row[2]/$sum);
print"</td><td class=\"text\"></td>";
}
print "</tr>\n";
}
print "<tr><td class=\"text\" colspan=\"3\" align=\"center\"><input type=submit value=\"Submit\"></form></td></tr>";
print "</table>\n";
}
print "</body></html>";
?>
Would anyone be willing to help me?