DÜNAAMILINE POLL
Laseb hääletada korra minutis, kasutab kontrolliks minut aega kehtivat küpsist.Küsimus ja vastusevariandid asuvad HTML koodis.
Hääletanute arv käib failis POLL_RESULT.TXT kujul 1||2||3 ning IP-d salvestatakse
faili POLLIP.TXT - kui POLL.PHP alguses $onevote=1; määrata, siis samalt IP-lt
teist korda hääletada ei saa.
POLL.PHP võtab parameetriks kasutaja hääle, salvestab tulemused ja genereerib
graafiku, XMLHTTP päringuga paigutatakse see käigupealt sisulehele.
Failid: POLL.HTML POLL.PHP
poll.html
<!-- hääletus //-->
<div id="kysitlus" style="margin-left:2px;font-size:1.1em;line-height:14px;width:400px;">
Kas klikatava pealkirja alla peidetud uudiste süsteem meeldib? <br>
<div id="poll" style="font-size:11px;line-height:14px;width:180px;float:right;">
<script>
function getVote(int) { // IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } // IE6, IE5
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ document.getElementById("poll").innerHTML=xmlhttp.responseText; }
}
xmlhttp.open("GET","poll.php?vote="+int,true); xmlhttp.send(); } getVote(-1);
</script>
</div> <!-- END poll //-->
<!-- vastusevalikud //-->
<div id="vastused" style="margin-left:10px;margin-bottom:4px;margin-top:10px;
padding:0px;font-size:12px;line-height:14px;width:220px;">
<form>
<input type="radio" name="vote" onclick="getVote(0)"> Meeldib, mugav ja ökonoomne <br>
<input type="radio" name="vote" onclick="getVote(1)"> Ei meeldi, vana oli selgem <br>
<input type="radio" name="vote" onclick="getVote(2)"> Kasutamine on probleemne <br>
</form>
</div> <!-- END vastused //-->
</div> <!-- END kysitlus //-->
<br>
poll.php
<?php
$vote = $_REQUEST['vote'];
// salvestab hääle küpsisesse
function kypsis ($nimi, $jutt, $aeg) {
$aeg = $aeg+time();
setcookie ($nimi, $jutt, $aeg);
}
// laeb häältefaili
$filename = "poll_result.txt";
if (file_exists($filename)) {
$content = file($filename);
} else { $content = "0||0||0"; }
// jagab hääled eraldi
$array = explode("||", $content[0]);
$yes = intval($array[0]);
$no = intval($array[1]);
$maybe = intval($array[2]);
// salvestab muudatused, kui kasutajal küpsist pole
if (($vote > -1) && (!isset($_COOKIE['sutapoll']))) {
switch($vote) {
case 0: $yes++; break;
case 1: $no++; break;
case 2: $maybe++; break;
default: -1; break;
}
kypsis('sutapoll', $vote, 60); // jätab hääletaja minutiks meelde
$insertvote = $yes."||".$no."||".$maybe;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
}
?>
<table style="width:430px;font-size:0.95em;line-height:10px;padding:2px;line-height:0.65em">
<tr><td><img src="poll.gif" width='<?php if ($yes>0) { echo(100*round($yes/($no+$yes+$maybe),2)); }
?>'height='14'> <?php if ($yes<1) { echo "0"; } else { echo(100*round($yes/($no+$yes+$maybe),2)); }
?>% <font color="#777" size="0.8em">(<?php echo $yes; ?> häält)</font></td></tr>
<tr><td><img src="poll.gif" width='<?php if ($no>0) { echo(100*round($no/($no+$yes+$maybe),2)); }
?>'height='14'> <?php if ($no<1) { echo "0"; } else { echo(100*round($no/($no+$yes+$maybe),2)); }
?>% <font color="#777" size="0.8em">(<?php echo $no; ?> häält)</font></td></tr>
<tr><td><img src="poll.gif" width='<?php if ($maybe>0) { echo(100*round($maybe/($no+$yes+$maybe),2)); }
?>'height='14'> <?php if ($maybe<1) { echo "0"; } else { echo(100*round($maybe/($no+$yes+$maybe),2)); }
?>% <font color="#777" size="0.8em">(<?php echo $maybe; ?> häält)</font></td></tr>
</table>


