Advertisement
intval

polls

Jul 1st, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.73 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. CREATE TABLE IF NOT EXISTS `polls_answers` (
  5.   `answer_id` mediumint(9) NOT NULL AUTO_INCREMENT,
  6.   `pollid` mediumint(9) DEFAULT NULL,
  7.   `text` varchar(255) DEFAULT NULL,
  8.   `votes` mediumint(9) DEFAULT '0',
  9.   PRIMARY KEY (`answer_id`),
  10.   KEY `answer_id` (`answer_id`,`pollid`)
  11. ) ENGINE=InnoDB  DEFAULT CHARSET=utf8
  12.  
  13.  
  14.  
  15. CREATE TABLE IF NOT EXISTS `polls_votes` (
  16.   `ip` varchar(15) NOT NULL,
  17.   `pollid` mediumint(9) DEFAULT NULL,
  18.   KEY `ip` (`ip`,`pollid`)
  19. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  20. */
  21.  
  22.  
  23. require 'configurationInit.php';
  24.  
  25. // fetch user's IP address
  26. $ip = $db->real_escape_string($_SERVER['REMOTE_ADDR']);
  27.  
  28.  
  29. /**
  30.  * Returns json-encoded poll's choices and results
  31.  * @param int $pollid Poll's ID
  32.  * @param bool $return either return value(true) or output value(false)
  33.  * @return json json-encoded poll results (text and number of votes)
  34.  */
  35. function show_poll_results($pollid, $return = false)
  36. {
  37.    
  38.     $votes = get_array("SELECT `text`, `votes` FROM `polls_answers` WHERE `pollid`=".intval($pollid));
  39.  
  40.     if($return) return json_encode($votes);
  41.    
  42.     // output the resulting javascript
  43.     send_user_script($pollid, $votes);
  44.    
  45. }
  46.  
  47.  
  48.  
  49. function send_user_script(&$pollid, &$data, $action = 'show_results')
  50. {
  51.     // outputs the javascript with the data
  52.     echo
  53.     "
  54.     var polls_data = polls_data || [];
  55.     polls_data[$pollid] = { action: '$action', data: ".json_encode($data)."};
  56.    ";
  57. }
  58.  
  59.  
  60.  
  61. // Submit poll vote
  62. if(
  63.         isset($_POST['poll'], $_POST['selection']) &&
  64.         ($pollid    = filter_input(INPUT_POST, 'poll',      FILTER_VALIDATE_INT)) &&
  65.         ($selection = filter_input(INPUT_POST, 'selection', FILTER_VALIDATE_INT))
  66. ):
  67.  
  68.  
  69.     // check whether the user had already voted to this poll
  70.     $has_votedR = $db->query("SELECT `ip` FROM `polls_votes` WHERE `ip`='$ip' AND `pollid`=$pollid LIMIT 1");
  71.     $has_voted = $has_votedR->num_rows > 0;
  72.     $has_votedR->close();
  73.  
  74.     // if the user had already voted for this poll - abort
  75.     if($has_voted)
  76.     {
  77.         show_poll_results($pollid, true);
  78.         die();
  79.     }
  80.                
  81.     if
  82.     (
  83.         $db->query("UPDATE `polls_answers` SET `votes` = `votes`+1
  84.                    WHERE `answer_id`=$selection AND `pollid`=$pollid")
  85.     )
  86.     {
  87.         $db->query("INSERT INTO `polls_votes` (`ip`, `pollid`) VALUES ('$ip', $pollid) ");
  88.     }
  89.    
  90.     // send the user the results of the poll including his own result
  91.     die(show_poll_results($pollid, true));
  92.  
  93.    
  94.  
  95. elseif
  96. (
  97.     isset($_GET['show_results']) &&
  98.     ($pollid = filter_input(INPUT_GET, 'show_results', FILTER_VALIDATE_INT))
  99. ):
  100.  
  101.  
  102.     show_poll_results($pollid);
  103.    
  104.    
  105.  
  106. else: // Show poll questions
  107.  
  108.    
  109.    
  110.     $pollid = filter_input(INPUT_GET, 'pollid', FILTER_VALIDATE_INT);
  111.     if(!$pollid || $pollid < 1 ) die('hm');
  112.  
  113.     // if the user has already voted for this poll - show poll results
  114.     // if he hadn't yet - show the possible choices to vote for
  115.     $has_votedR = $db->query("SELECT `ip` FROM `polls_votes` WHERE `ip`='$ip' AND `pollid`=$pollid LIMIT 1");
  116.     $has_voted = $has_votedR->num_rows > 0;
  117.     $has_votedR->close();
  118.    
  119.     if($has_voted)
  120.     {
  121.         show_poll_results($pollid, false);
  122.         die();
  123.     }
  124.  
  125.    
  126.     // fetch poll's data
  127.         // fetch poll's data
  128.     $pollR = $db->query("SELECT * FROM `polls_answers` WHERE `pollid` = $pollid");
  129.     if($pollR->num_rows < 1) die();
  130.  
  131.     //generate array of options with corresponding id's
  132.     while( $answer = $pollR->fetch_assoc() )
  133.     {
  134.         $options[$answer['answer_id']] = $answer['text'];
  135.     }
  136.     $pollR->close();
  137.  
  138.     // output the resulting javascript
  139.     send_user_script($pollid, $options, $action = 'run_poll');
  140.  
  141. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement