PHPBuilder - Simple polls & aquestionair & image post no db



RSS Twitter
Snippets Voting

Simple polls & aquestionair & image post no db

by: Guy
|
December 13, 2001

Version: 1

Type: Full Script

Category: Voting

License: Other

Description: This neat little program will enable to customize a poll & questionaair with an option to upload an image. Also poll statistics chart is provided. No bd connection required, all text file based. Bewarn; there is a small security bridge that can easily resolved with posting user HTML poll. PHP 4.0



<!-- INDEX.PHP -->

<table border="0" cellspacing="5" cellpadding="5" bgcolor="#0066CC" width="300"><tr>
<form enctype="multipart/form-data" action="index.php" method="post">
<td><font face="arial" size="2" color="#ffffff">
<b>Poll participation</b>
<p>
<?php
/*
written by Guy O. www.feathernetwork.com
no copy rights, but an affiliate advertising would be appreciated

1. rename files
copy_of_index.php to index.php
copy_of_new.php to new.php

2. create subfolders
/images
/db

3. In /db subfolder create a text file name
poll.ini
and place the digit 0 in it

4. change $root to whatever root folder also in new.php

5. MUST create at least first poll or else an error will occure (new.php)
*/
$root = "d:\\polls\\";

//open total poll count
$buffer = "";
$fn = $root . "db\\polls.ini";
$fs = filesize($fn);
$fp = fopen ($fn, "r");
while (!feof($fp)) {
	$buffer .= fread($fp,$fs);
}
fclose ($fp);
//generating random poll #
if ($buffer == "0" || $buffer == "1"){
	$magic_num = 1;
}
else {
	mt_srand ((double) microtime() * 1000000);
	$magic_num = mt_rand (1,$buffer);
}
//opening random poll
$buffer = "";
$fn = $root . "db\\poll$magic_num.txt";
$fs = filesize($fn);
$fp = fopen ($fn, "r");
while (!feof($fp)) {
	$buffer .= fread($fp,$fs);
}
fclose ($fp);

print $buffer;
?>
<p><input name="submit" type="submit" value="Submit">
<?php
//***** saving submitted poll ******
if ($submit){
	print "<hr><b>Results from previous poll:</b><br>";
//open poll results file
	$buffer = "";
	$fn = $root . "db\\result$id.txt";
	$fs = filesize($fn);
	$fp = fopen ($fn, "r");
	while (!feof($fp)) {
		$buffer .= fread($fp,$fs);
	}
	fclose ($fp);
//split the result string into array
	if($buffer == "0"){
		$buffer = $vote;
	}
	else {
		$buffer .= ",$vote";
	}
	$complete_votes = array();
	$complete_votes = split(',',$buffer);
//count the votes
	$num = count($complete_votes);
//calculate average
	$ave = array_sum($complete_votes) / $num;
//creates array with count of votes
	$answer = array_count_values($complete_votes);
//cut string into first digit
	$ave = substr($ave,0,3);
//writing poll new results
	$fn = $root . "db\\result$id.txt";
	$fs = filesize($fn);
	$fp = fopen ($fn, "w");
	fputs($fp,$buffer);
	fclose ($fp);
//print results
	print "<p><font size=\"3\"><b>You voted ";
	switch($type)
	{
		case 1;
			if ($vote == 1) print "No/Disagree ($vote)";
			else print "Yes/Agree ($vote)";
		break;
		case 2;
			if ($vote == 1) print "No/Disagree ($vote)";
			elseif ($vote == 2) print "Undecided ($vote)";
			else print "Yes/Agree ($vote)";
		break;
		default;
			print "$vote";
		break;
	}
	print "</b></font><br>&nbsp;<table width=\"160\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#0099ff\"><tr><td colspan=\"3\"><font face=\"arial\" size=\"1\" color=\"#ffffff\">&nbsp;Average votes ";
	switch($type)
	{
		case 1;
			if (round($ave) == 1) print "No/Disagree";
			else print "Yes/Agree";
			print " ($ave)</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#cc0000\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[1] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#669900\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[2] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
		break;
		case 2;
			if (round($ave) == 1) print "No/Disagree";
			elseif (round($ave) == 2) print "Undecided";
			else print "Yes/Agree";
			print " ($ave)</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#cc0000\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[1] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#cc99ff\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[2] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#669900\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[3] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
		break;
		default;
			print " ($ave)</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#7e0000\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[1] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#760600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[2] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#6a0e00\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[3] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#5e1a00\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[4] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#502300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[5] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#432d00\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[6] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#363800\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[7] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#294200\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[8] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#1b4c00\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[9] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
			print "</font></td></tr><tr><td colspan=\"2\" width=\"100\"><table bgcolor=\"#105500\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"";
			$temp = $answer[10] * 100 / $num;
			print round($temp);
			print "\"><tr><td><img src=\"images/spacer_clear.gif\" width=1 height=8></td></tr></table></td><td><font face=\"arial\" size=\"1\" color=\"#ffffff\">";
			print substr($temp,0,5)."%";
		break;
	}
	print "</font></td></tr><tr><td colspan=\"3\" align=\"right\"><font face=\"arial\" size=\"1\" color=\"#ffffff\">Total $num votes&nbsp;</font></td></tr></table>";
}
?>
</font>
</td></tr>
</form>
</table>
<p>
<a href="new.php"><b>Create a poll</b></a><br>&nbsp;
<p>
<a href="index.php">Next Poll</a>







<!-- NEW.PHP -->

<a href="index.php"><b>Answer a Poll</b></a>
<p>
<?php
if ($submit){
?>
<table border="0" cellspacing="5" cellpadding="5" bgcolor="#0066CC" width="300"><tr>
<td>
<font face="arial" size="2" color="#ffffff"><b>This is how your poll will look like:</b>
<p>
<?php
// written by Guy O. www.feathernetwork.com
// no copy rights, but an affiliate advertising would be appreciated

//SEE INDEX.PHP for details "how to"...

	$root = "d:\\polls\\";

	$poll = "";
	$msg = "";
	$buffer = "";
	$msg .= "<p><font color=\"red\"><b>";
	if (($content) && $type != 0)
	{
		$poll .= $content;
		if (is_uploaded_file($userfile))
		{
			if (file_exists($userfile))
			{
				if (file_exists($root . "images\\$userfile_name"))
				{
					$msg .= "Error: File with same name already exist.<br>";
				}
				else
				{
					move_uploaded_file($userfile, $root . "images\\$userfile_name");
					if (file_exists($root . "images\\$userfile_name"))
					{
						$msg .= "<font color=\"green\">File uploaded successfully.<br>";
						$poll .= "<br><img src=\"images/$userfile_name\">";
					}
				}
			}
			else
			{
			$msg .= "Error: File was not uploaded (doesn't exist).<br>";
			}
		}
		else
		{
			$msg .= "No file was uploaded.<br>";
		}
		$poll .= "<p>";
		switch($type)
		{
			case 1;
				$poll .= "<input type=\"hidden\" name=\"type\" value=\"1\"><input type=\"radio\" name=\"vote\" value=\"1\" checked> No/Disagree<input type=\"radio\" name=\"vote\" value=\"2\"> Yes/Agree ";
			break;
			case 2;
				$poll .= "<input type=\"hidden\" name=\"type\" value=\"2\"><input type=\"radio\" name=\"vote\" value=\"1\"> No/Disagree <input type=\"radio\" name=\"vote\" value=\"2\" checked> Undecided <input type=\"radio\" name=\"vote\" value=\"3\"> Yes/Agree";
			break;
			default;
				$poll .= "<input type=\"hidden\" name=\"type\" value=\"3\">Rate it: 1<input type=\"radio\" name=\"vote\" value=\"1\"><input type=\"radio\" name=\"vote\" value=\"2\"><input type=\"radio\" name=\"vote\" value=\"3\"><input type=\"radio\" name=\"vote\" value=\"4\"><input type=\"radio\" name=\"vote\" value=\"5\" checked><input type=\"radio\" name=\"vote\" value=\"6\"><input type=\"radio\" name=\"vote\" value=\"7\"><input type=\"radio\" name=\"vote\" value=\"8\"><input type=\"radio\" name=\"vote\" value=\"9\"><input type=\"radio\" name=\"vote\" value=\"10\">10";
			break;
		}
//get poll #
		$fn = $root . "db\\polls.ini";
		$fs = filesize($fn);
		$fp = fopen ($fn, "r");
		while (!feof($fp)) {
			$buffer .= fread($fp,$fs);
		}
		fclose ($fp);
//increment poll #
		$buffer++;
		$poll .= "<input type=\"hidden\" name=\"id\" value=\"$buffer\">";
		print $poll;
//save poll #
		$fn = $root . "db\\polls.ini";
		$fs = filesize($fn);
		$fp = fopen ($fn, "w");
		fputs($fp,$buffer);
		fclose ($fp);
//save poll db txt file
		$fn = $root . "poll$buffer.txt";
		$fs = filesize($fn);
		$fp = fopen ($fn, "w");
		fputs($fp,$poll);
		fclose ($fp);
//writing empty result file
		$fn = $root . "db\\result$buffer.txt";
		$fs = filesize($fn);
		$fp = fopen ($fn, "w");
		fputs($fp,"0");
		fclose ($fp);
	}
	else
	{
		$msg .= "Please fill in the content and type of poll properly.<br>";
	}
	$msg .= "</b></font>";
	print $msg;
?>
</font>
</td></tr>
</table>
<?php
}
else
{
?>
<table border="0" cellspacing="5" cellpadding="5" bgcolor="#0066CC" width="300"><tr>
<form enctype="multipart/form-data" action="new.php" method="post">
<td><input type="hidden" name="MAX_FILE_SIZE" value="1000000"><font face="arial" size="2" color="#ffffff">
<b>Create a poll</b>
<p>
Poll content:<br>
<textarea name="content" cols="27" rows="5"></textarea><br>
<select name="type">
<option value="0" selected>Type of poll:
<option value="1">Yes/Agree No/Disagree
<option value="2">Yes/Agree No/Disagree Undecided
<option value="3">Rate it (1 to 10)
</select><p>
Image attachment:<br>
<input name="userfile" size="23" type="file">
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="submit" type="submit" value="Submit poll">

</font>
</td></tr>
</form>
</table>
<?php
}
?>
<p>
<b><a href="new.php">Create a new poll</a> or back to <a href="index.php">Main polls page</a></b>
<p>
<b>POLL RULES:</b><br>
<font size="1">
No offensive content<br>
Poll content must have some text<br>
Type of poll has to be selected<br>
Uploading file is NOT required<br>
File dimention should be upto 300 pixel wide<br>
File size is limited to 1MB<br>
File will not be uploaded if file of the same name already exists
</font>
</font>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Guy

Comment:



Comment:

(Maximum characters: 1200). You have characters left.