001
<?php
002
// disable warnings
003
if
(version_compare(phpversion(), "5.3.0", ">=") == 1)
004
error_reporting(E_ALL & ~E_NOTICE &
~E_DEPRECATED);
005
else
006
error_reporting(E_ALL & ~E_NOTICE);
007
008
require_once('classes/CMySQL.php');
// including service class to work with database
009
010
if ($_POST['do'] ==
'vote') { // in case if we submitted poll
011
$iPollId = (int)$_POST['id'];
012
$iAnswer = (int)$_POST['answer'];
013
014
if ($iPollId && $iAnswer >= 0
&& ! isset($_COOKIE['av' . $iPollId])) {
015
// get poll info
016
$aPollInfo =
$GLOBALS['MySQL']->getRow("SELECT * FROM `s183_polls` WHERE `id` =
'{$iPollId}'");
017
018
// updating of poll results
019
$aAnswers = explode('<sep>',
$aPollInfo['answers']);
020
$iCnt = count($aAnswers);
021
$aVotes = ($aPollInfo['results'] ==
'') ? array_fill(0, $iCnt, 0) : explode('<sep>',
$aPollInfo['results']);
022
$aVotes[$iAnswer]++;
023
$iVotesCount = array_sum($aVotes);
024
$sVotes = implode('<sep>',
$aVotes);
025
$GLOBALS['MySQL']->res("UPDATE
`s183_polls` SET `results` = '{$sVotes}', `total_votes` = {$iVotesCount}
WHERE `id` = {$iPollId}");
026
027
// recalculation of percents
028
$iVotesCnt =
$aPollInfo['total_votes'] + 1;
029
$aPercents = array();
030
foreach ($aAnswers as $i =>
$sAnswer) {
031
$aPercents[$i] = round( (0 !=
$iVotesCnt ? (( $aVotes[$i] / $iVotesCnt ) * 100) : 0), 1);
032
}
033
034
setcookie('av' . $iPollId, '1',
time() + 24*3600, '/'); // easy protection from duplicate votes
035
036
// return back to JS
037
echo json_encode($aPercents);
038
exit;
039
} else {
040
exit;
041
}
042
}
043
044
$sCode = '';
045
$iItemId =
(int)$_GET['id'];
046
if ($iItemId) { //
View item output
047
$aItemInfo =
$GLOBALS['MySQL']->getRow("SELECT * FROM `s183_polls` WHERE `id` =
'{$iItemId}'"); // get poll info
048
049
$aAnswers = explode('<sep>',
$aItemInfo['answers']);
050
$iCnt = count($aAnswers);
051
$aVotes = ($aItemInfo['results'] == '') ?
array_fill(0, $iCnt, 0) : explode('<sep>', $aItemInfo['results']);
052
053
$iVotesCnt = $aItemInfo['total_votes'];
054
055
$sAnswers = '';
056
foreach ($aAnswers as $i => $sAnswer)
{
057
$fPercent = round((0 != $iVotesCnt ?
(($aVotes[$i] / $iVotesCnt) * 100) : 0), 1);
058
$sAnswers .= "<div
id='{$i}'><div>{$sAnswer} (<span>{$aVotes[$i]}</span>)</div><div
class='row'
style='width:{$fPercent}%'>{$fPercent}%</div></div>";
059
}
060
061
ob_start();
062
?>
063
<h1><?=
$aItemInfo['title'] ?></h1>
064
<h3><?=
date('F j, Y', $aItemInfo['when']) ?></h3><hr />
065
<div
class="answers">= $sAnswers ?></div>
066
<hr
/><h3><a href="<?= $_SERVER['PHP_SELF']
?>">back</a></h3>
067
<script>
068
$(function(){
069
$('.answers > div').click(function ()
{
070
071
var answer = $(this).attr('id');
072
var $span = $(this).find('span');
073
074
$.post('<?= $_SERVER['PHP_SELF']
?>', {id: = $iItemId ?>, answer: answer, do: 'vote'},
075
function(data){
076
if (data) {
077
var da = eval('(' + data
+ ')');
078
for (var p in da) {
079
$($('.answers >
div .row')[p]).animate({
080
width: da[p] +
"%"
081
}, 500);
082
$($('.answers >
div .row')[p]).text(da[p] + "%");
083
}
084
$span.text(parseInt($span.text()) + 1);
085
}
086
}
087
);
088
});
089
});
090
</script>
091
<?
092
$sCode .= ob_get_clean();
093
} else {
094
$sCode .= '<h1>List of
polls:</h1>';
095
096
// taking info about all polls from
database
097
$aItems =
$GLOBALS['MySQL']->getAll("SELECT * FROM `s183_polls` ORDER by `when`
ASC");
098
foreach ($aItems as $i => $aItemInfo)
{
099
$sCode .= '<h2><a
href="'.$_SERVER['PHP_SELF'].'?id='.$aItemInfo['id'].'">'.$aItemInfo['title'].'</a></h2>';
100
}
101
}
102
103
?>
104
<!DOCTYPE
html>
105
<html
lang="en" >
106
<head>
107
<meta charset="utf-8"
/>
108
<title>Creating own ajax poll
system | Script Tutorials</title>
109
110
<link
href="css/main.css" rel="stylesheet"
type="text/css" />
111
<script
type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
112
</head>
113
<body>
114
<div
class="container">
115
= $sCode ?>
116
</div>
117
<footer>
118
<h2>Creating own own ajax
poll system</h2>
119
<a href="http://www.script-tutorials.com/creating-own-ajax-poll-system/"
class="stuts">Back to original tutorial on <span>Script
Tutorials</span></a>
120
</footer>
121
</body>
122
</html>
|