|
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 19:36 UTC
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse
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 19:42 UTC
|
|
|
|
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 19:49 UTC
|
|
|
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 20:23 UTC
|
|
|
|
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 21:09 UTC
— Ed. 2010 Sep 18 at 21:33 UTC
|
|
|
|
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 22:34 UTC
— Ed. 2010 Sep 18 at 22:53 UTC
|
|
|
|
I can't tell what's going on from the snippets. Can you post all the relevant file(s)? Code & data?
|
|
|
|
≡
|
2010 Sep 21 at 02:16 UTC
|
|
|
|
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"> </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"> </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">© Sprinkles</div> </div> <div align="center"> <object width="1280" height="745"><param name="movie" value="http://www.youtube.com/v/PiHOonldMPU?fs=1&hl=en_US&rel=0&color1=0x3a3a3a&color2=0x999999&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&hl=en_US&rel=0&color1=0x3a3a3a&color2=0x999999&hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="1280" height="745"></embed></object> </div> </body> </html>
|
|
|
|
≡
|
2010 Sep 21 at 02:25 UTC
|
|
|
|
[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 07:55 UTC
|
|
|
|
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 08:41 UTC
— Ed. 2010 Sep 21 at 08:52 UTC
|
|
|
Outcast
My points value is a hilarious example of numerical humor.
2009 Dec 13 • 567
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 14:15 UTC
|
|
|
|
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 20:59 UTC
— Ed. 2010 Sep 21 at 21:01 UTC
|
|
|
|
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 21:13 UTC
— Ed. 2010 Sep 21 at 21:13 UTC
|
|
|
|
|
|
|
|
≡
|
2010 Sep 21 at 21:24 UTC
|
|
|
Page [1]
|