View Single Post
12-11-2005, 10:03 PM
#1
Shawon is offline Shawon
Shawon's Avatar
Status: Request a custom title
Join date: Nov 2004
Location: New York City
Expertise:
Software:
 
Posts: 1,164
iTrader: 0 / 0%
 

Shawon is on a distinguished road

Send a message via AIM to Shawon Send a message via MSN to Shawon

  Old  PHP Poll Problem

Hey, having a little problem with my poll, wondering if anyone can help.

So I have this poll, I didn't write it, it was a free script. It's at http://www.nflxl.com/ (on the right side).

The problem is when you vote it doesn't do anything. It used to work on my old server, but recently I moved to a new server and it doesn't work. Same thing happened with my shoutbox and comment system, but I got those fixed, but not sure how to fix this poll.

The votes go into a txt file, and yes I do have it CHMODed to 777 so I dunno what's wrong. There are 3 files: index.php, poll.block.php, and poll.class.php. Here are the php codes:

index.php
PHP Code:
<?php
    
require("poll.class.php");
    
$my_poll = new Poll();
    
    
$question "Which team has had the most unexpected season so far?";
    
$answers = array("Patriots","Bengals","Giants","Cowboys","Packers");
    
$datafile "poll/polls/unexpected.txt";

    
$my_poll->set($question$answers$datafile);
    
$my_poll->create();
?>
poll.block.php
PHP Code:
<?php
    
if($cmd == "vote"){
        if(!isset(
$pollblock) && !headers_sent()){
        
setcookie("pollblock""block"0"/");
    }
    }
?>
poll.class.php
PHP Code:
<?php
class Poll {
    var 
$questions;
    var 
$answers;
    var 
$datafiles;
    function 
Poll(){
        
$this->questions = array();
        
$this->answers = array();
        
$this->datafiles = array();
    }
    function 
set($question$answer$datafile){
        
array_push($this->questions$question);
        
array_push($this->answers$answer);
        
array_push($this->datafiles$datafile);
    }
    function 
create(){
        
$PHP_SELF $_SERVER['PHP_SELF'];
        global 
$PHP_SELF$HTTP_POST_VARS$pollblock$cmd;
        if(
$cmd == "vote"){
        
// ----------------------------------------------------------------------
        // * DISPLAY RESULTS *
                
                
if(isset($pollblock)){
                echo 
"<p>Sorry, it appears that you have already voted.</p>";
                return;
            }
            for(
$i 0$i count($this->questions); $i ++){
                
$question $this->questions[$i];
                
$answer $this->answers[$i];
                
$datafile $this->datafiles[$i];
                
$vote $HTTP_POST_VARS["poll".$i];
                
$data file($datafile);
                
                
// Populate default values - only happens on first vote
                
if(!$data[0]){
                    
$fp fopen($datafile"w");
                    
$defaultvalues "";
                    for(
$k 0$k count($answer); $k ++){
                        
$defaultvalues .= "0\n";
                    }
                    
flock($fp1);
                    
fputs($fp$defaultvalues);
                    
flock($fp3);
                    
fclose($fp);
                }
                
                
// Write votes to datafile
                
if(isset($vote)){
                    
$oldvotes $data[$vote];
                    
$oldvotes preg_replace("/\n\r*/"""$oldvotes);
                    
$data[$vote] = ($oldvotes 1)."\n";
                    
$file implode(""$data);
                    
$fp fopen($datafile"w");
                    
flock($fp1);
                    
fputs($fp$file);
                    
flock($fp3);
                    
fclose($fp);
                }
                
                
// Display Results
                
$total 0;
                while(list(
$key$val) = each($data)){
                    
$total += $val;
                }
                echo 
"<h2>$question</h2>\n";
                echo 
"<p>\n";
                while(list(
$key$val) = each($answer)){
                    
$votes preg_replace("/\n\r*/"""$data[$key]);
                    
$percent = ($votes 0) ? round(($votes/$total)*100) : 0;
                    if(!
$votes$votes 0;
                    echo 
""$answer[$key] ." <span class=\"winner\">("$votes .")</span><br />\n";
                }
                echo 
"</p>\n";
            }
        
// ----------------------------------------------------------------------
        
}else{
        
// ----------------------------------------------------------------------
        // * DISPLAY POLL *
            
            
echo "<form action=\"$php_self\" method=\"post\">\n";
            echo 
"<input type=\"hidden\" name=\"cmd\" value=\"vote\" />\n";
            for(
$i 0$i count($this->questions); $i ++){
                
$question $this->questions[$i];
                
$answer $this->answers[$i];
                echo 
"<h2>$question</h2>\n";
                echo 
"<p>";
                while(list(
$key$val) = each($answer)){
                    echo 
"<input type=\"radio\" class=\"radio\" name=\"poll$i\" value=\"$key\" />$val<br />\n";
                }
                echo 
"</p>\n";
            }
            echo 
"<div align=\"center\"><input type=\"submit\" class=\"submit\" name=\"vote\" value=\"Vote\" /></div>\n";
            echo 
"</form>\n";
        
        
// ----------------------------------------------------------------------
        
}
    }
}
?>