Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
OK, I'm trying to make a poll. I want it to check a database of IPs so a person can only vote once. I want to have a results file that it reads in, displays and updates based on the person's vote.

Here's what I have so far:
PHP:
<?php

$pollname = wb;

//get the IP address
$ip = getenv("REMOTE_ADDR");

//connect to the database
$db = mysql_connect("localhost", "brian_vote", "password here"); 
mysql_select_db("brian_vote", $db);

//check to see if the IP exists
$grab="select * from winterbeard where ipaddy='$ip'";
$ipresult=mysql_query($grab) or die("select  fails");
$ipstatus=mysql_num_rows($ipresult);

//access the results
$filename = $pollname . "results.txt";
$inf = fopen($filename, "r");
$results = fread($inf, filesize($filename));

//extract the info
while(!feof($inf))
{
$results .= fgets($inf, 1024);
}

fclose($inf);

//separate the info into yes/no
$votes = explode("\r\n", $results);

$yes = $votes[0];
$no = $votes[1];

//display current results (testing purposes)
if (!$vote) {
echo ("yes: $yes");
echo ("<br>");
echo ("no: $no");
}

//if the ip doesn't exist, display the poll options
if ($ipstatus==0)
{
//poll html
echo ("<p>
<form method=\"POST\" name=\"winterbeardvote\" action=\"$PHP_SELF\"> \n
<input type=\"radio\" name=\"vote\" value=\"yes\">Yes!<br> \n
<input type=\"radio\" name=\"vote\" value=\"no\">No!<br> \n
<input type=\"submit\" VALUE=\"Vote!\"> \n
</p>");

//when the person votes update the results
if ($vote)
{

//update the results
if ($vote=="yes")
{
$yes++;
}

else
{
$no++;
}

//update the file (all errors/warnings when I try it)
$outf=fopen($filename, "w+");
fwrite($outf, $yes, $no);
fclose($outf);

//add the ip to prevent multiple votes
$addy= "insert into winterbeard (ipaddy) values ('$ip')";
$insert = mysql_query($addy);

//display new results
echo ("<br>");
echo ("yes: $yes");
echo ("<br>");
echo ("no: $no");
echo ("<p>Thanks for voting!</p>");

}

}

//if the ip address does exist, don't display the poll
else
{
echo ("yes: $yes");
echo ("<br>");
echo ("no: $no");
echo("<p>Thanks for voting!</p>");
}

?>
I've commented a lot of it so hopefully that'll help. I feel like 90% of it works because:

1) it reads the current results in from the wbresults.txt file.

2) it checks the mysql database for the users ip address

3) if the ip is not present, it allows the person to vote and it increments the count

4) if the person has voted (i.e. the ip address is present in the database) it displays the results, but won't let them vote.


the problem: the 3 statements involving the updating of the wbresults.txt file.

you can try it out if you want, but warning, YOU'LL ONLY GET ONE SHOT! :)

http://www.brianellisrules.com/temp/vote.inc - the code I included in this message

http://www.brianellisrules.com/temp/wbresults.txt - the results file

moved - the script (with my default page header). If you go here, it'll allow you to vote, but once you do vote, 3 warnings pop up (and it increments the count/results). if you hit reload, the warnings go away and the old count (from the file) is displayed (because the ip address exists and it doesn't allow the person the option of voting).

This is long. My problem: writing to a file. I know, it's basic... but I've fought with this thing on and off all day and I figured I'd put this up before I went to sleep.

Thanks, youn's are the best.
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
Any reason not to put the vote alongside the IP in the MySQL DB? If it was me I'd keep all the collected data in one place...

Other than that...make sure the permissions on the text file are set to be world writable (usually 777).
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
Hmmm, so each IP address would have the vote counted next to it... intriguing. ;) Then I'd just have to have the script add up the votes in each column and display those...

I'll look into that... and to be honest, I thought about doing that at first, but for whatever reason opted to go to the text file (probably just because I figured it'd be easier to have one number rather than add a column of numbers).
 

winwintoo

macrumors 6502
Nov 26, 2003
291
0
"Real" programmers almost never keep an accumulated total of anything. It's more reliable to add up the column when you want to display the total.

Keeping a running total is dicey. When is the latest value added to the total, what if 2+ values are added at the same time, which one gets added first - can that affect decision making. What if a new value is added to the total and then the rest of the transaction fails for some reason and the total is not re-adjusted.....

I could go on. Bottom line, add up the column when you want to display the total.

m
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
done and done. sort of.

right now I just have it record a 'yes' or 'no' in a column next to the ip address. it adds it up and displays the number of yes's and no's correctly.

now I just need to figure out how to get rid of the voting options once a vote has been cast.... babysteps.

edit: here's my new code:
PHP:
<?php

//get the IP address
$ip = getenv("REMOTE_ADDR");

//connect to the database
$db = mysql_connect("localhost", "brian_vote", "password here"); 
mysql_select_db("brian_vote", $db);

//check to see if the IP exists
$grabip="select * from winterbeard where ipaddy='$ip'";
$ipresult=mysql_query($grabip) or die("select  fails");
$ipstatus=mysql_num_rows($ipresult);

//if the ip doesn't exist, display the poll options
if ($ipstatus==0)
{

//poll html
echo ("<p>
<form method=\"POST\"  name=\"winterbeardvote\" action=\"$PHP_SELF\"> \n
<input type=\"radio\" name=\"vote\" value=\"yes\">Yes!<br> \n
<input type=\"radio\" name=\"vote\" value=\"no\">No!<br> \n
<input type=\"submit\" VALUE=\"Vote!\"> \n
</p>");

//when the person votes update the results
if ($vote)
{
//add the ip to prevent multiple votes
$info= "insert into winterbeard (vote, ipaddy) values ('$vote','$ip')";
$insertinfo = mysql_query($info);

//check vote numbers from database
$grabyes="select * from winterbeard where vote='yes'";
$yesresult=mysql_query($grabyes);
$yes=mysql_num_rows($yesresult);

$grabno="select * from winterbeard where vote='no'";
$noresult=mysql_query($grabno);
$no=mysql_num_rows($noresult);

$totalvotes=$yes+$no;
$yespercent=$yes/$totalvotes*100;
$nopercent=100-$yespercent;

//display the results
echo ("yes: $yespercent% ($yes votes)");
echo ("<br>");
echo ("no: $nopercent% ($no votes)");
echo ("<p>Thanks for voting!</p>");

}
}

//if the ip address does exist, don't display the poll options
else
{
//check vote numbers from database
$grabyes="select * from winterbeard where vote='yes'";
$yesresult=mysql_query($grabyes);
$yes=mysql_num_rows($yesresult);

$grabno="select * from winterbeard where vote='no'";
$noresult=mysql_query($grabno);
$no=mysql_num_rows($noresult);

$totalvotes=$yes+$no;
$yespercent=$yes/$totalvotes*100;
$nopercent=100-$yespercent;

//display the results
echo ("yes: $yespercent% ($yes votes)");
echo ("<br>");
echo ("no: $nopercent% ($no votes)");
echo ("<p>Thanks for voting!</p>");
}

?>

still trying to figure out how to get rid of the option to vote once a vote has been cast... any ideas?
 

arson

macrumors member
Jan 12, 2004
84
2
Minnesota
Here is a more thorough IP check...

PHP:
<?
	if(getenv("HTTP_CLIENT_IP")) { 
	
		$ip = getenv("HTTP_CLIENT_IP"); 
	
	} elseif(getenv("HTTP_X_FORWARDED_FOR")) { 
	
		$ip = getenv("HTTP_X_FORWARDED_FOR"); 
	
	} else { 
		
		$ip = getenv("REMOTE_ADDR"); 
	
	}
?>
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
Hey, thanks for the response. I added that snippet to my code... but, that's not my problem (I should've been more clear, sorry). Hopefully this will help.


Before (first page load, no vote has been cast):
vote1.jpg


During (option has been selected, vote has been cast. you can see the results are updated below the voting options, yet the voting options remain.)
vote2.jpg


After (page reloaded after a vote had been previously cast. everything appears correctly.)
vote3.jpg



I hope that helps clear things up. Basically, I'd like to have the voting options not displayed in the second picture (after an initial vote has been cast, but the page hasnt' been reloaded).
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
Ignore this post, for I am a php-guru.





I will offer up my coding services at a mere $500/hour.

I know, it's a deal... consider this a discount for friends & family! ;)
 

arson

macrumors member
Jan 12, 2004
84
2
Minnesota
Originally posted by brianellisrules
Hey, thanks for the response. I added that snippet to my code... but, that's not my problem (I should've been more clear, sorry). Hopefully this will help.
Looks like you figured it out, but this is how I do it:


PHP:
<?
if ($_SERVER[REQUEST_METHOD] == "POST") {

//process the vote and display overall results




} else {

// display poll options


}
?>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.