Poll Problem

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
slimjim
Forum Newbie
Posts: 2
Joined: Thu May 22, 2003 9:18 am

Poll Problem

Post by slimjim »

Ok i been trying to figure out whats wrong now for days and I dont get it.
Heres how it should work:

Person goes to the poll page
That person votes
Then that person gets a cookie
and then gets redirected to the results page. ( Which works )

(Not working)
Now if that person comes back to that page
That person sees a message that they have voted already
But also now he see's results but not able to vote

Cookie set to expire after 24 hours.

When person goes back to poll page, results table is showing but no results. I also have an error.
Warning: Variable passed to each() is not an array or object in /home/virtual/site3/fst/var/www/html/aaa/clients/poll.php on line 9

Now i do get the message that i voted.

If anyone feels like they can help me out here please do.

Code: Select all

<?php
//include "../tpl/clients_top.ihtml";
include "../conf/sys.php";
//First checks for cookies
if ($_COOKIE['Voted'] == 'Yes'){
	//include "../tpl/clients_top.ihtml";
    echo ("<center>Hey idiot, didn't I say to vote once a day?</center>");
// display results if cookie is found
     while (list($key, $val) = each($file_array)) {
                  $total += $val;
              }
              echo "<center>\n";
              echo "<h3>$pollname Poll vote results :</h3>";
              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)) {
					if ($total > 0)
						{  $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></center>";
              }
              echo "</TABLE><br>";    
}else{
    if (! $vote && ! $result) {
// first time no vote and no result so display the form if no cookie found
         echo "<center>\n";
         echo "<FORM METHOD="POST">\n";
         echo "<TABLE WIDTH=50% 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="left"><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></center>";
    } else {

         $file_array = file($RESULT_FILE_NAME); // or error("Can not open \$RESULT_FILE_NAME");
//  Save result after voting
         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("$RESULT_FILE_NAME", "w"); //or error("Can not write \$RESULT_FILE_NAME");
             flock($fp, 1);
             fputs($fp, $file);                                                     
             flock($fp, 3);
             fclose($fp);
             setcookie ("Voted", "Yes", time() + 86400);
// finished voting so set cookie
              echo "<center><b>Your Vote has been recorded</b></center>\n";                         
         }
//  Display result after voting
         while (list($key, $val) = each($file_array)) {
             $total += $val;
         }
         echo "<center>\n";
         echo "<h3>$pollname Poll vote results :</h3>";
         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></center>";
         }
         echo "</TABLE><br>";
    }    
}
//end else
//include "../tpl/clients_bottom.ihtml";
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

line 9 is
while (list($key, $val) = each($file_array)) {
?
you have

Code: Select all

if (! $vote && ! $result) {
... 
$file_array = file($RESULT_FILE_NAME); // or error("Can not open \$RESULT_FILE_NAME");
but no such thing in the first block...
slimjim
Forum Newbie
Posts: 2
Joined: Thu May 22, 2003 9:18 am

Poll

Post by slimjim »

Your right duh
What i was missing was the line below. put it on top and Wa-La it worked.
Where were you earlier LOL
Thanks Volka

Code: Select all

$file_array = file($RESULT_FILE_NAME); // or error("Can not open \$RESULT_FILE_NAME");
Post Reply