Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Hi,
A friend gave me a simple 'poll' script which saves the info to a txt file, not MySQL or anyother database.
I put two polls in ONE page without using tables and frames. But I get a problem. When I vote, lets say option 1 on the first poll, and when I get re-directed to the results, I get another vote on option 1 on the first AND SECOND poll.
How can I fix this problem?
Here is my script:Code: Select all
<?php
include 'includes/pnAPI.php';
pnInit();
include 'includes/legacy.php';
// eugenio themeover 20020413
// pnThemeLoad();
define("LOADED_AS_MODULE","1");
# includes the mainfile functions and the header junk
if (!isset($mainfile)) { include("mainfile.php"); }
include("header.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center">
<h1><font face="Verdana, Arial, Helvetica, sans-serif">Other Artists:</font></h1>
<p><font face="Verdana, Arial, Helvetica, sans-serif">Polls:</font></p>
<?php
//////////////////////////
// THIS IS VOTE NUMBER 1//
//////////////////////////
$RESULT_FILE_NAME = "poll_data2.txt";
$QUESTION = "Best Artist to fly with?";
$ANSWER = array("FatBoy Slim", "Greenday", "Bom Funk Mc.",);
$IMG_DIR_URL = "./vote";
$REVOTE_TIME = 3600;
if (! $vote && ! $result) {
echo "<FORM METHOD="POST">\n";
echo "<TABLE WIDTH=60% BORDER=1><TR><TD><TABLE WIDTH="100%" BORDER=0>\n";
echo "<TR><TH>$QUESTION</TH></TR>\n";
while (list($key, $val) = each($ANSWER)) {
echo "<TR><TD align="center"><INPUT TYPE="radio" NAME="answer" VALUE="$key"> $val</TD></TR>\n";
}
echo "<TR><TD align="center"><INPUT TYPE="Submit" NAME="vote" VALUE=" Vote "></TD></TR>\n";
echo "<TR><TD align="center"><INPUT TYPE="Submit" NAME="result" VALUE=" See Result "></TD></TR>\n";
echo "</TABLE></TD></TR></TABLE></FORM>";
} else {
$file_array = file($RESULT_FILE_NAME);
if ($answer < count($ANSWER) && $vote) {
if (count($file_array) < count($ANSWER)) {
$file_array = array("0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n");
}
$old_answer = $file_array[$answer];
$old_answer = preg_replace("/\n\r*/", "", $old_answer);
$file_array[$answer] = ($old_answer + 1)."\n";
$file = join('', $file_array);
$fp = fopen("$RESULT_FILE_NAME", "w");
flock($fp, 1);
fputs($fp, $file);
flock($fp, 3);
fclose($fp);
echo "rate saved";
}
while (list($key, $val) = each($file_array)) {
$total += $val;
}
echo "<h2>PHP Poll vote results :</h2>";
echo "<TABLE CELLSPACING=2 CELLPADDING=1 BORDER=1>";
echo "<tr><th>What</th><th>Percentage</th><th>Votes</th></tr>";
while (list($key, $val) = each($ANSWER)) {
$percent = $file_array[$key] * 100 / $total;
$percent_int = floor($percent);
$percent_float = number_format($percent, 1);
$tp += $percent_float;
echo "<tr><td> $ANSWER[$key] </td><td><img height=9 src="$IMG_DIR_URL/vote_left.gif"><img height=9 width="$percent_int" src="$IMG_DIR_URL/vote_middle.gif"><img height=9 src="$IMG_DIR_URL/vote_right.gif"> $percent_float % </td><td>$file_array[$key]</td></tr>";
}
echo "</TABLE><br>";
}
?>
<p> </p>
<p>
<?php
//////////////////////////
// THIS IS VOTE NUMBER 2//
//////////////////////////
$RESULT_FILE_NAME = "poll_data3.txt";
$QUESTION = "Best Songs to Fly With?";
$ANSWER = array("Fat Boy Slim - Right Here, Right Now", "Fat Boy Slim - Praise You", "Bom Funk Mc - Free Styler", "Enya - Adiemus", "U2 - Mission Impossible");
$IMG_DIR_URL = "./vote";
$REVOTE_TIME = 3600;
if (! $vote && ! $result) {
echo "<FORM METHOD="POST">\n";
echo "<TABLE WIDTH=60% BORDER=1><TR><TD><TABLE WIDTH="100%" BORDER=0>\n";
echo "<TR><TH>$QUESTION</TH></TR>\n";
while (list($key, $val) = each($ANSWER)) {
echo "<TR><TD align="center"><INPUT TYPE="radio" NAME="answer" VALUE="$key"> $val</TD></TR>\n";
}
echo "<TR><TD align="center"><INPUT TYPE="Submit" NAME="vote" VALUE=" Vote "></TD></TR>\n";
echo "<TR><TD align="center"><INPUT TYPE="Submit" NAME="result" VALUE=" See Result "></TD></TR>\n";
echo "</TABLE></TD></TR></TABLE></FORM>";
} else {
$file_array = file($RESULT_FILE_NAME);
if ($answer < count($ANSWER) && $vote) {
if (count($file_array) < count($ANSWER)) {
$file_array = array("0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n");
}
$old_answer = $file_array[$answer];
$old_answer = preg_replace("/\n\r*/", "", $old_answer);
$file_array[$answer] = ($old_answer + 1)."\n";
$file = join('', $file_array);
$fp = fopen("$RESULT_FILE_NAME", "w");
flock($fp, 1);
fputs($fp, $file);
flock($fp, 3);
fclose($fp);
echo "rate saved";
}
while (list($key, $val) = each($file_array)) {
$total += $val;
}
echo "<h2>PHP Poll vote results :</h2>";
echo "<TABLE CELLSPACING=2 CELLPADDING=1 BORDER=1>";
echo "<tr><th>What</th><th>Percentage</th><th>Votes</th></tr>";
while (list($key, $val) = each($ANSWER)) {
$percent = $file_array[$key] * 100 / $total;
$percent_int = floor($percent);
$percent_float = number_format($percent, 1);
$tp += $percent_float;
echo "<tr><td> $ANSWER[$key] </td><td><img height=9 src="$IMG_DIR_URL/vote_left.gif"><img height=9 width="$percent_int" src="$IMG_DIR_URL/vote_middle.gif"><img height=9 src="$IMG_DIR_URL/vote_right.gif"> $percent_float % </td><td>$file_array[$key]</td></tr>";
}
echo "</TABLE><br>";
}
?>
</p>
<p> </p></div>
</body>
</html>
<?php
// include footer junk
include("footer.php");
?>feyd | Help us, help you. Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]