Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- CREATE TABLE IF NOT EXISTS `polls_answers` (
- `answer_id` mediumint(9) NOT NULL AUTO_INCREMENT,
- `pollid` mediumint(9) DEFAULT NULL,
- `text` varchar(255) DEFAULT NULL,
- `votes` mediumint(9) DEFAULT '0',
- PRIMARY KEY (`answer_id`),
- KEY `answer_id` (`answer_id`,`pollid`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
- CREATE TABLE IF NOT EXISTS `polls_votes` (
- `ip` varchar(15) NOT NULL,
- `pollid` mediumint(9) DEFAULT NULL,
- KEY `ip` (`ip`,`pollid`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- */
- require 'configurationInit.php';
- // fetch user's IP address
- $ip = $db->real_escape_string($_SERVER['REMOTE_ADDR']);
- /**
- * Returns json-encoded poll's choices and results
- * @param int $pollid Poll's ID
- * @param bool $return either return value(true) or output value(false)
- * @return json json-encoded poll results (text and number of votes)
- */
- function show_poll_results($pollid, $return = false)
- {
- $votes = get_array("SELECT `text`, `votes` FROM `polls_answers` WHERE `pollid`=".intval($pollid));
- if($return) return json_encode($votes);
- // output the resulting javascript
- send_user_script($pollid, $votes);
- }
- function send_user_script(&$pollid, &$data, $action = 'show_results')
- {
- // outputs the javascript with the data
- echo
- "
- var polls_data = polls_data || [];
- polls_data[$pollid] = { action: '$action', data: ".json_encode($data)."};
- ";
- }
- // Submit poll vote
- if(
- isset($_POST['poll'], $_POST['selection']) &&
- ($pollid = filter_input(INPUT_POST, 'poll', FILTER_VALIDATE_INT)) &&
- ($selection = filter_input(INPUT_POST, 'selection', FILTER_VALIDATE_INT))
- ):
- // check whether the user had already voted to this poll
- $has_votedR = $db->query("SELECT `ip` FROM `polls_votes` WHERE `ip`='$ip' AND `pollid`=$pollid LIMIT 1");
- $has_voted = $has_votedR->num_rows > 0;
- $has_votedR->close();
- // if the user had already voted for this poll - abort
- if($has_voted)
- {
- show_poll_results($pollid, true);
- die();
- }
- if
- (
- $db->query("UPDATE `polls_answers` SET `votes` = `votes`+1
- WHERE `answer_id`=$selection AND `pollid`=$pollid")
- )
- {
- $db->query("INSERT INTO `polls_votes` (`ip`, `pollid`) VALUES ('$ip', $pollid) ");
- }
- // send the user the results of the poll including his own result
- die(show_poll_results($pollid, true));
- elseif
- (
- isset($_GET['show_results']) &&
- ($pollid = filter_input(INPUT_GET, 'show_results', FILTER_VALIDATE_INT))
- ):
- show_poll_results($pollid);
- else: // Show poll questions
- $pollid = filter_input(INPUT_GET, 'pollid', FILTER_VALIDATE_INT);
- if(!$pollid || $pollid < 1 ) die('hm');
- // if the user has already voted for this poll - show poll results
- // if he hadn't yet - show the possible choices to vote for
- $has_votedR = $db->query("SELECT `ip` FROM `polls_votes` WHERE `ip`='$ip' AND `pollid`=$pollid LIMIT 1");
- $has_voted = $has_votedR->num_rows > 0;
- $has_votedR->close();
- if($has_voted)
- {
- show_poll_results($pollid, false);
- die();
- }
- // fetch poll's data
- // fetch poll's data
- $pollR = $db->query("SELECT * FROM `polls_answers` WHERE `pollid` = $pollid");
- if($pollR->num_rows < 1) die();
- //generate array of options with corresponding id's
- while( $answer = $pollR->fetch_assoc() )
- {
- $options[$answer['answer_id']] = $answer['text'];
- }
- $pollR->close();
- // output the resulting javascript
- send_user_script($pollid, $options, $action = 'run_poll');
- endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement