please help me sort out this script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

please help me sort out this script

Post by volkan »

feyd | Help us, help you. Please use

Code: Select all

and

Code: 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>&nbsp;</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>&nbsp;</p></div>
</body>
</html>

<?php
// include footer junk
include("footer.php");
?>

feyd | Help us, help you. Please use

Code: Select all

and

Code: 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

add a hidden element in each form to let you know which poll is being answered.

oh, and turn off register_globals.
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

sorry, what do you mean by Hidden Element?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<input type="hidden" name="id" value="poll#1" />
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

I still get the same, this is what I did:

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>
  <p><input type="hidden" name="id" value="poll#1" /></p>
  <?php
     //////////////////////////
	// THIS IS VOTE NUMBER 1//
   //////////////////////////
   
// Necessary Variables:

$RESULT_FILE_NAME = "poll_data2.txt";
	// En: Absolute path and name to file contain poll data.
	

$QUESTION = "Best Artist to fly with?";
	// En: Question Text.

$ANSWER = array("FatBoy Slim", "Greenday", "Bom Funk Mc.",);
	// En: All answer.
	

$IMG_DIR_URL = "./vote";
	// En: URL Directory of poll graphs.


$REVOTE_TIME = 3600;
	// En: Time (second) after people can revote, use cookies.


// End  Necessary Variables section
/******************************************************************************/

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); // or error("Can not open \$RESULT_FILE_NAME");

	// En: Save result
	
	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"); //or error("Can not write \$RESULT_FILE_NAME");
		flock($fp, 1);
		fputs($fp, $file);                                                     
		flock($fp, 3);
		fclose($fp);
		echo "rate saved";
	}

	// En: Display result
	
	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>";
}

?>

 <input type="hidden" name="id" value="poll#2" />
 
  <p>
    <?php
	 //////////////////////////
	// THIS IS VOTE NUMBER 2//
   //////////////////////////

// Necessary Variables:

$RESULT_FILE_NAME = "poll_data3.txt";
	// En: Absolute path and name to file contain poll data.
	

$QUESTION = "Best Songs to Fly With?";
	// En: Question Text.

$ANSWER = array("Fat Boy Slim - Right Here, Right Now", "Fat Boy Slim - Praise You", "Bom Funk Mc - Free Styler", "Enya - Adiemus", "U2 - Mission Impossible");
	// En: All answer.
	

$IMG_DIR_URL = "./vote";
	// En: URL Directory of poll graphs.


$REVOTE_TIME = 3600;
	// En: Time (second) after people can revote, use cookies.


// End  Necessary Variables section
/******************************************************************************/

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); // or error("Can not open \$RESULT_FILE_NAME");

	// En: Save result
	// Fr: Enregistre le resultat
	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"); //or error("Can not write \$RESULT_FILE_NAME");
		flock($fp, 1);
		fputs($fp, $file);                                                     
		flock($fp, 3);
		fclose($fp);
		echo "rate saved";
	}

	// En: Display result
	// Fr: Affiche le resultat
	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>&nbsp;</p></div>
</body>
</html>

<?php
// include footer junk
include("footer.php");
?>
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

uh oh! u had to place that <input type="hidden"... line in side the form. then it will be read by the checking script else how will ya know which poll was submitted.

ps:and plz use php and code tags only where necessary. it's really annoying to scroll thru those blank line...
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

Ok,
When I do:

Code: Select all

//////////////////////////
	// 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;

<input type="hidden" name="id" value="poll#1" />

if (! $vote && ! $result) {
I get:
Parse error: parse error in /home/volkan/TXGN7HR0/htdocs/html/o.php on line 47
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

culprit lies on line #34

Code: Select all

$ANSWER = array("FatBoy Slim", "Greenday", "Bom Funk Mc.",); //see the , at the end
correct that.

AND place ur <input type="hidden" ... line after this line

Code: Select all

echo "<FORM METHOD="POST">\n";
and then see wht happens...
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

mmmm,
Same error differant line.
I did this:

Code: Select all

echo "<FORM METHOD="POST">\n";
	<input type="hidden" name="id" value="poll#1" />
	echo "<TABLE WIDTH=60% BORDER=1><TR><TD><TABLE WIDTH="100%" BORDER=0>\n";
and corrected the ','


Parse error: parse error in /home/volkan/TXGN7HR0/htdocs/html/o.php on line 51
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the trailing comma in the array doesn't cause anything.

Not echoing the text I gave you did. Remember, HTML isn't script, and PHP isn't html.
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

feyd wrote:the trailing comma in the array doesn't cause anything.
Hi
Sorry, i dont get what that means!
I tried this:

Code: Select all

echo "<FORM METHOD="POST">\n";
	echo <input type="hidden" name="id" value="poll#1"/>
	echo "<TABLE WIDTH=60% BORDER=1><TR><TD><TABLE WIDTH="100%" BORDER=0>\n";
and i got:



i tried

Code: Select all

echo "<FORM METHOD="POST">\n";
	echo <input type="hidden" name="id" value="poll#1"/>;
	echo "<TABLE WIDTH=60% BORDER=1><TR><TD><TABLE WIDTH="100%" BORDER=0>\n";
and i got:


Parse error: parse error, expecting `','' or `';'' in /home/volkan/TXGN7HR0/htdocs/html/o.php on line 51


same!
please help!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php
echo <input type="hidden" name="id" value="poll#1"/>;
?>
should be

Code: Select all

<?php
echo "<input type="hidden" name="id" value="poll#1"/>";
?>
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

ok, script works, but I get the same problem. How do I turn the glodals off again??
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

feyd wrote:oh, and turn off register_globals.
there are two ways:

1. google for it.
2. check out the php manual
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

ohhh!
Dam,
This page is not on my server.
It is on a remote server. I don't have
access to php.ini.

Is there another way?
Post Reply