Voting

Voting

sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
So I have a video of the day page. I want users to be able to vote whether they like it or not. How can I do it in php? Is it as simple as variable++ then display the variable?
 
 
 
2010 Sep 18 at 12:36 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
It can be as simple or as complicated as you want, really. Sounds odd but the other way is to test things.
 
 
 
2010 Sep 18 at 12:42 PDT
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
Haha, I'm so lazy. I jus' looked for a script online.
php code

<?php
/*************************************************
* Micro Polling System
*
* Version: 1.0
* Date: 2007-04-05
*
* Usage:
* Add your votings settings to polldata.txt file
* The first line is the question and the other
* linas are the possible answers.
*
****************************************************/

$pollQuestion = '';
$answers = '';

function
readData(){
global
$pollQuestion,$answers;
// Read configuration
$rawdata = file('polldata.txt');
// Get the question for polling
$pollQuestion = $rawdata[0];

// Get number of answers - The foirs row is the question
$numberOfAnswers = sizeof($rawdata)-1;
$count = 0;
for (
$i=1; $i <= $numberOfAnswers; $i++){
$answerData = explode(':',$rawdata[$i]);
// If tha actual row is not empty than add to the answers array
if (strlen(trim($answerData[0]))>0){
$answers[$count]['text'] = $answerData[0];
$answers[$count]['count'] = $answerData[1];
++
$count;
}
}
}

function
writeData(){
global
$pollQuestion,$answers;
$file = fopen('polldata.txt','w');
fwrite($file,$pollQuestion."\r\n",strlen($pollQuestion));
foreach (
$answers as $value) {
$row = $value['text'].':'.$value['count']."\r\n";
fwrite($file,$row,strlen($row));
}
fclose($file);
}



 
 
 
2010 Sep 18 at 12:49 PDT
buq25

2008 Jul 5 • 583
295 ₧
sprinkles said:
code
...text... $rawdata ...text...


Retard information?
Today's post brought to you by the letter: "heck".
 
 
 
2010 Sep 18 at 13:23 PDT
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
The problem now is its not reading the file correctly.

Its reading the file correctly now, but something is still wrong.

php code

<?php
$blacklist
="";
$file = fopen ("Data/Poll/blacklist.txt", "r");
$ln= 0;
while (
$line= fgets ($file)) {
++
$ln;
if(
fgets($file) == $_SERVER['REMOTE_ADDR'])
{
$blacklist = "true";
}
if (
$line===FALSE) print ("FALSE\n");
else print (
$line);
}
fclose($file);
?>


 
 
 
2010 Sep 18 at 14:09 PDT — Ed. 2010 Sep 18 at 14:33 PDT
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
So I figured it out.
Its not a reading problem, but a writing problem. It writes to the file the ip, then puts a whitespace, after that it line breaks. So its saying they don't match up.
Any ideas on how to remove the whitespace?
Here's the line that writes to the file:
php code

<?php
fwrite
($file2,$_SERVER['REMOTE_ADDR']."\r\n");
?>



Well that problem is solved now.

New problem! It wont fucking line break. Same code as above.
 
 
 
2010 Sep 18 at 15:34 PDT — Ed. 2010 Sep 18 at 15:53 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
I can't tell what's going on from the snippets. Can you post all the relevant file(s)? Code & data?
 
 
 
2010 Sep 20 at 19:16 PDT
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
Entire file:
php code

<?php
$pollQuestion
= '';
$answers = '';

function
readData(){
global
$pollQuestion,$answers;
// Read configuration
$rawdata = file('Data/Poll/polldata.txt');
// Get the question for polling
$pollQuestion = $rawdata[0];

// Get number of answers - The foirs row is the question
$numberOfAnswers = sizeof($rawdata)-1;
$count = 0;
for (
$i=1; $i <= $numberOfAnswers; $i++){
$answerData = explode(':',$rawdata[$i]);
// If tha actual row is not empty than add to the answers array
if (strlen(trim($answerData[0]))>0){
$answers[$count]['text'] = $answerData[0];
$answers[$count]['count'] = $answerData[1];
++
$count;
}
}
}

function
writeData(){
global
$pollQuestion,$answers;
$file = fopen('Data/Poll/polldata.txt','w');
fwrite($file,$pollQuestion."\r\n",strlen($pollQuestion));
foreach (
$answers as $value) {
$row = $value['text'].':'.$value['count']."\r\n";
fwrite($file,$row,strlen($row));
}
fclose($file);
$file2 = fopen('Data/Poll/blacklist.txt','a');
$ip = $_SERVER['REMOTE_ADDR'];
fwrite($file2,$ip."\n");
fclose($file2);
}

readData();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<link href="Data/Poll/style/style.css" rel="stylesheet" type="text/css" />
<title>Bamf.^ Video of the Day</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18602024-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>

</head>
<body bgcolor="#000000">
<div id="main">
<?php
$blacklist
="";
$ip = "";
$file = fopen ("Data/Poll/blacklist.txt", "r");
while (
$line = fgets($file))
{
if(
$line == $_SERVER['REMOTE_ADDR'])
{
$blacklist = "true";
break;
}
else print
$line."\n";
}
fclose($file);

if (!isset(
$_POST['submitBtn']) && $blacklist!="true") { ?>
<div class="caption"><?php echo $pollQuestion; ?></div>
<div id="icon">&nbsp;</div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="poll">
<table width="300">
<?php
foreach ($answers as $value) {
echo
'<tr><td><input type="radio" name="polling" value="'.$value['text'].'"/> '.$value['text'].'</td></tr>';
}
?>
<tr><td align="center"><br/><input class="text" type="submit" name="submitBtn" value="Vote" /></td></tr>
</table>
</form>
<?php
} else {
$count = 0;
foreach (
$answers as $value) {
if (
$value['text'] == $_POST['polling']) {
$answers[$count]['count'] = ((int)$value['count'])+1;
(int)
$totalCount++;
}
++
$count;
}

writeData();
?>
<div class="caption">Thanks for your vote!</div>
<div id="icon">&nbsp;</div>
<div id="result">
<table width="300">
<?php
foreach ($answers as $value) {
echo
'<tr><td> '.$value['text'].'</td><td>'.$value['count'].'</td></tr>';
}
?>
</table>
</div>
<?php } ?>
<div id="source">&#169; Sprinkles</div>
</div>
<div align="center">
<object width="1280" height="745"><param name="movie" value="http://www.youtube.com/v/PiHOonldMPU?fs=1&amp;hl=en_US&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/PiHOonldMPU?fs=1&amp;hl=en_US&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="1280" height="745"></embed></object>
</div>
</body>
</html>

 
 
 
2010 Sep 20 at 19:25 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
[2010-09-21 07:35:07 UTC] <superjer> K I'm looking.
[2010-09-21 07:45:59 UTC] <superjer> It works. Sort of. On my server.
[2010-09-21 07:46:01 UTC] <superjer> http://superjer.com/lies/vote.php
[2010-09-21 07:46:13 UTC] <superjer> I had to create the data files and give the webserver write permission to them.
[2010-09-21 07:48:33 UTC] <superjer> This isn't a very good script. It adds extra junk characters for some reason. Also it's not concurrency safe. If two users vote at nearly the same time, it can hose the data file.
[2010-09-21 07:52:37 UTC] <superjer> 1. Get rid of the \r's
[2010-09-21 07:53:20 UTC] <superjer> 2. change the blacklist check to trim($line) == $_SERVER['REMOTE_ADDR']
 
 
 
2010 Sep 21 at 00:55 PDT
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
Sorry I was AFK. I added the trim($line) == $_SERVER['REMOTE_ADDR'] and it seems to work!? Haven't tested it out much, or looked at any files/resources. So idk right now. I'll let you know later.


So I have tested it out and it works. Now to jus' clean it up a bit. And how do you supposed I make it concurrency safe?
 
 
 
2010 Sep 21 at 01:41 PDT — Ed. 2010 Sep 21 at 01:52 PDT
Outcast
My points value is a hilarious example of numerical humor.

2009 Dec 13 • 566
80,085 ₧
sprinkles said:
So I have a video of the day page. I want users to be able to vote whether they like it or not. How can I do it in php? Is it as simple as variable++ then display the variable?


Are you talkin about the "like" button you posted in the chat?
 
 
 
2010 Sep 21 at 07:15 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
sprinkles said:
And how do you supposed I make it concurrency safe?


Use a database instead.

-OR-

Make sure that no 2 processes can write to either file at the same time. You can do this with flock().

php code

<?php

$lockfile
= fopen("Data/Poll/lock.txt",'w');
flock($lockfile, LOCK_EX)

/* All of your code that reads/writes files . . . */

fclose($lockfile);

?>



Then make sure that your webserver has write access to Data/Poll/lock.txt
 
 
 
2010 Sep 21 at 13:59 PDT — Ed. 2010 Sep 21 at 14:01 PDT
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
I can't tell if its safe now, but flock hasn't broke anything. I am assuming its working. All I have to do is write (copy pasta rather) some code so it doesn't write the same ip over and over.

Thanks Superjer!
 
 
 
2010 Sep 21 at 14:13 PDT — Ed. 2010 Sep 21 at 14:13 PDT
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
 
 
 
2010 Sep 21 at 14:24 PDT
Page [1]