<?php
  session_start();
   
$fp = fopen("poll_question.ssi","r");
$sess_id=fread($fp,filesize("poll_question.ssi"));
fclose($fp);


   if( isset( $_SESSION['sessionnumber'] ) ) 
   { 
      if( $_SESSION['sessionnumber']==$sess_id)
      {
          $_SESSION['counter'] += 1;
      }
      else
      { 
         $_SESSION['counter'] = 1;
         $_SESSION['sessionnumber']=$sess_id;
      }
   }
   else 
   { 
         $_SESSION['sessionnumber'] = $sess_id;
         $_SESSION['counter'] = 1;
   }


$vote = $_REQUEST['vote'];

$fp = fopen("poll_mcoption0.ssi","r");
$mcoption0=fread($fp,filesize("poll_mcoption0.ssi"));
fclose($fp);
$fp = fopen("poll_mcoption1.ssi","r");
$mcoption1=fread($fp,filesize("poll_mcoption1.ssi"));
fclose($fp);
$fp = fopen("poll_mcoption2.ssi","r");
$mcoption2=fread($fp,filesize("poll_mcoption2.ssi"));
fclose($fp);
$fp = fopen("poll_mcoption3.ssi","r");
$mcoption3=fread($fp,filesize("poll_mcoption3.ssi"));
fclose($fp);



//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];
$maybe = $array[2];
$other = $array[3];

if ($_SESSION['counter']==1){
if ($vote == 0) {
  $yes = $yes + 1;
}
if ($vote == 1) {
  $no = $no + 1;
}
if ($vote == 2) {
  $maybe = $maybe + 1;
}
if ($vote == 3) {
  $other = $other + 1;
}


//insert votes to txt file

$insertvote = $yes."||".$no."||".$maybe."||".$other;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);}
{}
?>

<h2>Thanks for voting!</h2)>
<h3>The response from 
<?php echo($no+$yes+$maybe+$other); ?> 
participants is: </h3>
<table>
<tr>
<td><?php echo($mcoption0); ?></td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($yes/($no+$yes+$maybe+$other),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes+$maybe+$other),2)); ?>%
</td>
</tr>
<tr>
<td><?php echo($mcoption1); ?></td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($no+$yes+$maybe+$other),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes+$maybe+$other),2)); ?>%
</td>
</tr>
<tr>
<td><?php echo($mcoption2); ?></td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($maybe/($no+$maybe+$yes+$other),2)); ?>'
height='20'>
<?php echo(100*round($maybe/($maybe+$no+$yes+$other),2)); ?>%
</td>
</tr>
<tr>
<td><?php echo($mcoption3); ?></td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($other/($no+$maybe+$yes+$other),2)); ?>'
height='20'>
<?php echo(100*round($other/($other+$no+$yes+$other),2)); ?>%
</td>


</tr>
</table> 
<br><br><br>
Refresh bar chart <input type="radio" name="vote" value="100" onclick="getVote(this.value)">

