php Poll question

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

Post Reply
ThinkGeekness
Forum Newbie
Posts: 16
Joined: Sat May 17, 2003 8:00 pm

php Poll question

Post by ThinkGeekness »

I have found a poll called spiderPoll on Hotscripts. This poll is great, except when you vote it brings you to a different page to show you the results. I want it to be able to just show the results in the same part that you voted in. If you dont understand what I mean, go to http://www.thinkgeekness.com/poll/poll.html for an example.

The following is the code for the poll:

Code: Select all

<?

// DEFINE THE VARIABLES //
// title of this poll
$title = "This is a test";
// closing date for this poll in MM/DD/YYYY format
$closingDate = "7/25/2003";
// text file that stores vote choices and totals
$pollFile = "poll.txt";
// text file that stores IP addresses
$ipFile = "ips.txt";
// full path to your CSS style sheet
$styleSheet = "";

// DO NOT EDIT BELOW THIS POINT UNLESS YOU KNOW PHP! //

// DEFINE THE FUNCTIONS //
// check if the poll has closed
function is_closed() {
	global $closingDate;

	// split the closing date into month, day, and year
	$closingDate = explode("/", $closingDate);

	// get today's today to test against the closing date
	$today = getdate();

	$message = date("l, F j", mktime(0,0,0,$closingDate[0],$closingDate[1],$today[year]));

	// if today's year is greater than the closing year, return true
	if ($today[year] > $closingDate[2]) {
		return $message;
	}
	// if today's year is equal to the closing year
	elseif ($today[year] == $closingDate[2]) {
		// if today's month is greater than the closing month, return true
		if ($today[mon] > $closingDate[0]) {
			return $message;
		}
		// if today's month is equal to the closing month
		elseif ($today[mon] == $closingDate[0]) {
			// if today is greater than or equal to the closing day, return true
			if ($today[mday] >= $closingDate[1]) {
				return $message;
			}
			// if the poll is still open, return false
			else {
				return false;
			}
		}
		// if the poll is still open, return false
		else {
			return false;
		}
	}
	// if the poll is still open, return false
	else {
		return false;
	}
}

// check if the user has already voted
function has_voted() {
	global $ipFile;
	global $REMOTE_ADDR;

	// open the IP address file
	$ips = fopen($ipFile, "r");

	// compare each entry with the user's IP address
	while (!feof($ips)) {
		$ip = fgets($ips, 20);

		if ($ip == $REMOTE_ADDR . "\r\n") {
			$match = 1;
			break;
		}
	}

	// close the IP address file
	fclose($ips);

	if (!$match) {
		// reopen the IP address file
		$ips = fopen($ipFile, "a");

		// add the user's IP address
		fputs($ips, $REMOTE_ADDR . "\r\n");

		// close the IP address file
		fclose($ips);

		return false;
	}
	else {
		return true;
	}
}

// add the user's vote
function addVote($vote) {
	global $pollFile;

	// get the current votes
	$fp_read = fopen($pollFile, "r");
	$currentVote = fread($fp_read, filesize($pollFile));
	fclose($fp_read);

	// create an array with even numbers containing vote choices
	// and odds containing vote totals
	$votes = split('[|:]', $currentVote);

	// update the vote
	for ($i = 1; $i < count($votes); $i = $i + 2) {
		// get the array index number for the name of this vote
		$name = $i - 1;

		// if this vote choice is this user's selection, increment it
		if ($votes[$name] == $vote) {
			$votes[$i]++;
		}

		// if this vote IS the last choice
		if ($i == (count($votes) - 1)) {
			$updatedVote .= $votes[$name] . ":" . $votes[$i];
		}

		// if this vote is NOT the last choice
		else {
			$updatedVote .= $votes[$name] . ":" . $votes[$i] . "|";
		}
	}

	// save the updated vote
	$fp_write = fopen($pollFile, "w");
	fputs($fp_write, $updatedVote);
	fclose($fp_write);
}

// display the poll
function displayPoll($message) {
	global $title, $pollFile, $styleSheet;

	echo "<html>\n";
	echo "<head>\n";
	echo "<title>$title</title>\n";
	echo "<link rel="stylesheet" type="text/css" href="$styleSheet">\n";
	echo "</head>\n\n";
	echo "<body>\n";

	// get the current votes
	$fp_read = fopen($pollFile, "r");
	$currentVote = fread($fp_read, filesize($pollFile));
	fclose($fp_read);

	// create an array with even numbers containing vote choices
	// and odds containing vote totals
	$votes = split('[|:]', $currentVote);

	// if a message was sent, print it
	if (isset($message)) {
		echo "<p align=center>$message</p>\n\n";
	}

	echo "<table align=center>\n";
	echo "<caption align=top><b>$title</b></caption>\n";

	// print the poll table rows
	// including vote choice, vote total, and percentage of total votes
	for ($i = 1; $i < count($votes); $i = $i + 2)
	{
		// add together each vote total to find the total number of votes cast
		$totalVotes += $votes[$i];
	}

	for ($i = 1; $i < count($votes); $i = $i + 2) {
		// get the array index number for the name of this vote
		$name = $i - 1;

		// calculate the percentage of total votes for this vote
		// rounded to 1 decimal place
		if ($totalVotes == 0) {
			$percentage = 0;
		}
		else {
			$percentage = $votes[$i] / $totalVotes * 100;
			$percentage = round($percentage, 1);
		}

		echo "<tr>\n";
		echo "\t<td>$votes[$name]</td>\n";
		echo "\t<td>$votes[$i] votes</td>\n";

		// if the percentage is 0, don't print a bar
		if ($percentage == 0) {
			echo "\t<td>$percentage%</td>\n";
		}

		// otherwise, print the bar
		else {
			echo "\t<td><img src=poll.jpg width=$percentage height=15> $percentage%</td>\n";
		}

		echo "</tr>\n";
	}

	// print the total number of votes cast
	echo "<caption align=bottom>Total Votes: $totalVotes</caption>\n";

	// finish printing the poll table
	echo "</table>\n";
	echo "</body>\n";
	echo "</html>\n";
}


// PROGRAM CODE //
// if the poll is closed, display the poll and exit
if ($message = is_closed()) {
	displayPoll("The poll closed on " . $message . ".");
	exit;
}

// if the user is not voting, display the poll and exit
if (!isset($vote)) {
	displayPoll("");
	exit;
}

// if the user has already voted, display the poll and exit
if (has_voted()) {
	displayPoll("You have already voted.");
	exit;
}

// add the user's vote
addVote($vote);

// display the poll
displayPoll("");

?>
What can I change so that it displays the results on the same page instead of a different page?

Thanks
Flood
Forum Newbie
Posts: 11
Joined: Wed Jun 25, 2003 4:52 pm

Post by Flood »

Hi!

I am not sure I understand the question properly...
You want the same file to display both the vote operation and the results?
As far as I have seen in the link you provide, the vote operation is done via a HTML file, while the results of the poll are displayed thanks to a PHP file. You simply need to combine the two files, right?

To do so, I suggest something like this (not tested of course! :-) )


<?php

if (isSet($_POST['vote']) && in_array($_POST['vote'], array('Yes', 'No'))) { // vote received
// display the poll results

} else { // display the vote operation
// all the html lines of poll.html
}

?>


I do not know if it answers your question, but I hope so!

/Flood
ThinkGeekness
Forum Newbie
Posts: 16
Joined: Sat May 17, 2003 8:00 pm

Post by ThinkGeekness »

Thanks for your reply... You understood it correctly, I want it to display the results in the same file as the question (and in the same location as the question was asked.)

I have just started to learn php (class started Monday).. Where would I put the code that you gave me?

Thanks
Flood
Forum Newbie
Posts: 11
Joined: Wed Jun 25, 2003 4:52 pm

Post by Flood »

Hi!

Well I guess just put it in poll.php and you directly have to call this file in your browser... Simply get rid of poll.html...

Hope I am clear :-)

/Flood
ThinkGeekness
Forum Newbie
Posts: 16
Joined: Sat May 17, 2003 8:00 pm

Post by ThinkGeekness »

Hey, I guess I am not totally getting it... Would it look like:

Code: Select all

...
// add the user's vote
addVote($vote);

// display the poll
displayPoll("");

if (isSet($_POST&#1111;'vote']) && in_array($_POST&#1111;'vote'], array('Yes', 'No'))) &#123; // vote received 
// display the poll results 

&#125; else &#123; // display the vote operation 
<form action=poll.php method=post>
  <p>Questionp>
  <p align="left"><br>
    <input type=radio name=vote value="Option">
    Yes<br>
    <input type=radio name=vote value="Option">
    No</p>
  <p><br>
    <input name="submit" type=submit value=Vote>
  </p>
</form>// all the html lines of poll.html 
&#125; 
?>
Thanks
Flood
Forum Newbie
Posts: 11
Joined: Wed Jun 25, 2003 4:52 pm

Post by Flood »

Well...

Code: Select all

<?php
between

Code: Select all

if (isSet($_POST&#1111;'vote']) && in_array($_POST&#1111;'vote'], array('Yes', 'No'))) &#123; // vote received 
// display the poll results
and, you put your php code (originally from poll.php)
between

Code: Select all

&#125; else &#123; // display the vote operation 
// all the html lines of poll.html
and, you put your html code (originally from poll.html)

Code: Select all

&#125; 

?>
/Flood
Post Reply