voting codes, what is wrong?

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
User avatar
sean82
Forum Newbie
Posts: 8
Joined: Fri Apr 16, 2004 5:33 am

voting codes, what is wrong?

Post by sean82 »

hi people
i have a voting poll and the question table is ok but if you choose one and click on vote then the vote cant be saved and some wrong sentences appear..
i think something is wrong with this poll_data.txt...here are the codes of this poll_data.txt
please check if you can see the problem:

$RESULT_FILE_NAME = "poll_data.txt"; ഀ
// En: http://......de/poll/poll_data.txt ഀ
$QUESTION = "How do you like this Script?"; ഀ
// En: Question Text. ഀ
$ANSWER = array("Love it!", "Like it!", "Its okay..", "I dislike it", "I hate it.."); ഀ
// En: All answer. ഀ
$IMG_DIR_URL = "./vote"; ഀ
// En: http://......de/poll/voteഀ
$REVOTE_TIME = 3600; ഀ
// En: Time (second) after people can revote, use cookies. ഀ
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

For a start it looks like you're mixing up PHP and HTML in the txt file, you don't need the bits in there.

In order for the PHP in the txt file to be executed as actual PHP code you need to place <?php at the start and ?> at the end and then either include() or require() it.

It would also help to see your PHP code which deals with the txt file reading and writting because I'm guessing there is a problem with that as well.
User avatar
sean82
Forum Newbie
Posts: 8
Joined: Fri Apr 16, 2004 5:33 am

Post by sean82 »

may be the codes are wrong here they are:

Code: Select all

<?php
<?php 
// En: Begin PHP Code / Fr: Debut code PHP 

// Necessary Variables: 

$RESULT_FILE_NAME = "poll_data.txt"; 
   // En: http://....poll/poll_data.txt 
   // Fr: http://.....poll/poll_data.txt 

$QUESTION = "How do you like this Script?"; 
   // En: Question Text. 
   // Fr: Texte de la question. 
$ANSWER = array("Love it!", "Like it!", "Its okay..", "I dislike it", "I hate it.."); 
   // En: All answer. 
   // Fr: Reponses possibles 

$IMG_DIR_URL = "./vote"; 
   // En: http://.......de/poll/vote 
   // Fr: http://........de/poll/vote 

$REVOTE_TIME = 3600; 
   // En: Time (second) after people can revote, use cookies. 
   // Fr: Temps en second apres lequel une personne peut revoter. 

// End  Necessary Variables section 
/******************************************************************************/ 

if (! $vote && ! $result) { 
   echo "<FORM METHOD="POST">\n"; 
   echo "<TABLE WIDTH=100% BORDER=1><TR><TD><TABLE WIDTH="100%" BORDER=0>\n"; 
   echo "<TR><TH>$QUESTION</TH></TR>\n"; 
   while (list($key, $val) = each($ANSWER)) { 
      echo "<TR><TD align="center"><INPUT TYPE="radio" NAME="answer" VALUE="$key"> $val</TD></TR>\n"; 
   } 
   echo "<TR><TD align="center"><INPUT TYPE="Submit" NAME="vote" VALUE=" Vote "></TD></TR>\n"; 
   echo "<TR><TD align="center"><INPUT TYPE="Submit" NAME="result" VALUE=" See Result "></TD></TR>\n"; 
   echo "</TABLE></TD></TR></TABLE></FORM>"; 
} else { 

   $file_array = file($poll_data.txt); // or error("Can not open \$poll_data.txt"); 

   // En: Save result 
   // Fr: Enregistre le resultat 
   if ($answer < count($ANSWER) && $vote) { 
      if (count($file_array) < count($ANSWER))  { 
         $file_array = array("0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"); 
      } 
      $old_answer = $file_array[$answer]; 
      $old_answer = preg_replace("/\n\r*/", "", $old_answer); 
      $file_array[$answer] = ($old_answer + 1)."\n"; 

      $file = join('', $file_array); 
      $fp = fopen("$poll_data.txt", "r+"); //or error("Can not write \$poll_data.txt"); 
      flock($fp, 1); 
      fputs($fp, $file);                                                      
      flock($fp, 3); 
      fclose($fp); 
      echo "rate saved"; 
   } 

   // En: Display result 
   // Fr: Affiche le resultat 
   while (list($key, $val) = each($file_array)) { 
      $total += $val; 
   } 

   echo "<h2>PHP Poll vote results :</h2>"; 
   echo "<TABLE CELLSPACING=2 CELLPADDING=1 BORDER=1>"; 
   echo "<tr><th>What</th><th>Percentage</th><th>Votes</th></tr>"; 

   while (list($key, $val) = each($ANSWER)) { 
      $percent =  $file_array[$key] * 100 / $total; 
      $percent_int = floor($percent); 
      $percent_float = number_format($percent, 1); 
      $tp += $percent_float; 
      echo "<tr><td> $ANSWER[$key] </td><td><img height=9 src="$IMG_DIR_URL/vote_left.gif"><img height=9 width="$percent_int" src="$IMG_DIR_URL/vote_middle.gif"><img height=9 src="$IMG_DIR_URL/vote_right.gif"> $percent_float % </td><td>$file_array[$key]</td></tr>"; 
   } 

   echo "</TABLE><br>"; 
} 

?> 

?>
Post Reply