Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. CREATE TABLE tally (
  2. QID varchar(32) NOT NULL,
  3. AID integer NOT NULL,
  4. votes integer NOT NULL,
  5. PRIMARY KEY (QID,AID))
  6.  
  7. $mysql_host = "localhost";
  8. $mysql_database = "vote";
  9. $mysql_user = "root";
  10. $mysql_password = "";
  11.  
  12.  
  13. class webPoll {
  14.  
  15. # makes some things more readable later
  16. const POLL = true;
  17. const VOTES = false;
  18.  
  19. # number of pixels for 1% on display bars
  20. public $scale = 2;
  21.  
  22.  
  23. public $question = '';
  24. public $answers = array();
  25. private $header = '<form class="webPoll" method="post" action="%src%">
  26. <input type="hidden" name="QID" value="%qid%" />
  27. <h4>%question%</h4>
  28. <fieldset><ul>';
  29. private $center = '';
  30. private $footer = "n</ul></fieldset>%button%n</form>n";
  31. private $button = '<p class="buttons"><button type="submit" class="vote">Vote!</button></p>';
  32. private $md5 = '';
  33.  
  34. /**
  35. * ---
  36. * Takes an array containing the question and list of answers as an
  37. * argument. Creates the HTML for either the poll or the results depending
  38. * on if the user has already voted
  39. */
  40. public function __construct($params) {
  41. $this->question = array_shift($params);
  42. $this->answers = $params;
  43. $this->md5 = md5($this->question);
  44.  
  45. $this->header = str_replace('%src%', $_SERVER['SCRIPT_NAME'], $this- >header);
  46. $this->header = str_replace('%qid%', $this->md5, $this->header);
  47. $this->header = str_replace('%question%', $this->question, $this->header);
  48.  
  49. # seperate cookie for each individual poll
  50. isset($_COOKIE[$this->md5]) ? $this->poll(self::VOTES) : $this- >poll(self::POLL);
  51. }
  52. private function poll($show_poll) {
  53. $replace = $show_poll ? $this->button : '';
  54. $this->footer = str_replace('%button%', $replace, $this->footer);
  55.  
  56. # static function doesn't have access to instance variable
  57. if(!$show_poll) {
  58. $results = webPoll::getData($this->md5);
  59. $votes = array_sum($results);
  60. }
  61.  
  62. for( $x=0; $x<count($this->answers); $x++ ) {
  63. $this->center .= $show_poll ? $this->pollLine($x) : $this->voteLine($this->answers[$x],$results[$x],$votes);
  64. }
  65.  
  66. echo $this->header, $this->center, $this->footer;
  67. }
  68. private function pollLine($x) {
  69. isset($this->answers[$x+1]) ? $class = 'bordered' : $class = '';
  70. return "
  71. <li class='$class'>
  72. <label class='poll_active'>
  73. <input type='radio' name='AID' value='$x' />
  74. {$this->answers[$x]}
  75. </label>
  76. </li>
  77. ";
  78. }
  79. private function voteLine($answer,$result,$votes) {
  80. $result = isset($result) ? $result : 0;
  81. $percent = round(($result/$votes)*100);
  82. $width = $percent * $this->scale;
  83. return "
  84. <li>
  85. <div class='result' style='width:{$width}px;'>&nbsp;</div> {$percent}%
  86. <label class='poll_results'>
  87. $answer
  88. </label>
  89. </li>
  90. ";
  91. }
  92. /**
  93. * processes incoming votes. votes are identified in the database by a combination
  94. * of the question's MD5 hash, and the answer # ( an int 0 or greater ).
  95. */
  96. static function vote() {
  97. if(!isset($_POST['QID']) ||
  98. !isset($_POST['AID']) ||
  99. isset($_COOKIE[$_POST['QID']])) {
  100. return;
  101. }
  102. try{
  103. $dbh = new PDO('mysql:host=localhost;dbname=vote', 'root', '');
  104. $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  105. } catch(PDOException $e) {
  106. echo "Error: " . $e->getMessage() . "<br/>";
  107. }
  108.  
  109. try {
  110. $sth = $dbh->prepare("INSERT INTO tally (QID,AID,votes) values ('QID', 'AID', '1')" );
  111. $sth->execute(array($_POST['QID'],$_POST['AID']));
  112. }
  113. catch(PDOException $e) {
  114. # 23000 error code means the key already exists, so UPDATE!
  115. if($e->getCode() == 23000) {
  116. try {
  117. $sth = $dbh->prepare("UPDATE tally SET votes = votes + 1 WHERE QID='$QID' AND AID='$AID'");
  118. $sth->execute(array($_POST['QID'],$_POST['AID']));
  119. }
  120. catch(PDOException $e) {
  121. webPoll::db_error($e->getMessage());
  122. }
  123. }
  124. else {
  125. webPoll::db_error($e->getMessage());
  126. }
  127. }
  128.  
  129. # entry in $_COOKIE to signify the user has voted, if he has
  130. if($sth->rowCount() == 1) {
  131. setcookie($_POST['QID'], 1, time()+60*60*24*365);
  132. $_COOKIE[$_POST['QID']] = 1;
  133. }
  134. }
  135. static function getData($question_id) {
  136. try {
  137. $dbh = new PDO('mysql:host=localhost;dbname=vote', 'root', '');
  138. $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  139.  
  140. $STH = $dbh->prepare('SELECT AID, votes FROM tally WHERE QID = ?');
  141. $STH->execute(array($question_id));
  142. }
  143. catch(PDOException $e) {
  144. # Error getting data, just send empty data set
  145. webPoll::db_error($e->getMessage());
  146. return array(0);
  147. }
  148.  
  149. while($row = $STH->fetch()) {
  150. $results[$row['AID']] = $row['votes'];
  151. }
  152.  
  153. return $results;
  154. }
  155. /*
  156. * You can do something with the error message if you like. Email yourself
  157. * so you know something happened, or make an entry in a log
  158. */
  159. static function db_error($error) {
  160. echo "A database error has occured. $error";
  161. exit;
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement