<?php
/******************************************************************************\
* PHP Poll                                     Version 1.0                     *
* Copyright 2000 Frederic TYNDIUK (FTLS)       All Rights Reserved.            *
* E-Mail: tyndiuk@ftls.org                     Script License: GPL             *
* Created  02/28/2000                          Last Modified 02/28/2000        *
* Scripts Archive at:                          http://www.ftls.org/php/        *
*******************************************************************************/
// Necessary Variables:

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

$QUESTION = "which metacreature would you most like to wear on a shirt?";
	// Question Text.
$ANSWER = array('<img src="/pixels/pixelly/meta/frog.png" alt="the frog" />', '<img src="/pixels/pixelly/meta/sheep.png" alt="the sheep" />', '<img src="/pixels/pixelly/meta/giraffe.png" alt="the giraffe" />', '<img src="/pixels/pixelly/meta/kangaroo-poll.png" alt="the kangaroo" />', '<img src="/pixels/pixelly/meta/brontosaurus.png" alt="the brontosaurus" />', '<img src="/pixels/pixelly/meta/sasquatch.png" alt="the sasquatch" />', '<img src="/pixels/pixelly/meta/turtle.png" alt="the turtle" />');
	// All answers.

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

$REVOTE_TIME = 21600;
	// Time (seconds) after people can revote, using cookies (?).

// End Necessary Variables section

/******************************************************************************/

if (! $vote && ! $result) {
	echo "<form method=\"post\">\n";
	echo "<h1>$QUESTION</h1>\n";
	while (list($key, $val) = each($ANSWER)) {
		echo "<p>$val<span class=\"button\"><input type=\"radio\" name=\"answer\" value=\"$key\"></span></p>\n";
	} 
	echo "<p><input type=\"submit\" name=\"vote\" value=\"vote\"><br />\n";
	echo "<input type=\"submit\" name=\"result\" value=\"see results\"></p>\n";
	echo "</form>";
} else {

	$file_array = file($RESULT_FILE_NAME); // or error("Can not open \$RESULT_FILE_NAME");

	// 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 "<p class=\"saved\">vote saved, thanks!</p>\n";
	}

	// Display result
	while (list($key, $val) = each($file_array)) {
		$total += $val;
	}

	echo "<h1>which metacreature would you most like to wear on a shirt?</h1>\n\n";
	echo "<table cellspacing=\"0\" id=\"results\">\n";
	echo "<tr>\n<th>item</th>\n<th>percentage</th>\n<th>votes</th>\n</tr>\n";

	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>\n<td class=\"candidate\"> $ANSWER[$key] </td>\n<td><img width=\"5\" height=\"10\" src=\"bar.png\" /><img width=\"$percent_int\" height=\"10\" src=\"bar.png\" /> $percent_float% </td>\n<td>$file_array[$key]</td>\n</tr>\n";
	}

	echo "</table>";
}

?>