Easy to implement and adjust Poll Class


/ Published in: PHP
Save to your folder(s)

Very simple to use Web Poll Class, simply create a new instance, set the question and set the answers. You still have to set up your database yourself


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3.  * Requires database(web-poll) with a table(poll) with two fields, votes(int) and language(varchar)
  4.  */
  5. class Vote
  6. {
  7. public $question;
  8. public $answers = array();
  9. private $_db;
  10.  
  11. public function __construct($question)
  12. {
  13. //Set the question
  14. $this->question = $question;
  15.  
  16. //Connect to the database
  17. try
  18. {
  19. $this->_db = new PDO('mysql:host=localhost;dbname=web-poll', 'root', '');
  20. }
  21. catch(PDOException $e)
  22. {
  23. echo $e->getMessage();
  24. }
  25. }
  26.  
  27. //Insert the users vote and update the vote count
  28. public function insertVote($vote)
  29. {
  30. $stmt = $this->_db->prepare("UPDATE poll SET votes = votes + 1 WHERE language = ?");
  31. $stmt->execute(array($vote));
  32. }
  33.  
  34. //Set the answers
  35. public function setAnswers($answer)
  36. {
  37. $this->answers[] = $answer;
  38. }
  39.  
  40. //Display the vote field
  41. public function displayVote()
  42. {
  43. echo '<div class="poll">
  44. <p>'.$this->question.'</p>
  45. <form action="index.php" method="POST">
  46. ';
  47.  
  48. foreach($this->answers as $answer)
  49. {
  50. echo '<p><input name="vote" type="radio" value="'.$answer.'" /> '.$answer.'</p>';
  51. }
  52.  
  53. echo ' <input type="submit" value="Vote" />
  54. <a href="index.php?results=show"><input type="button" value="Show results" /></a>
  55. </form>
  56. </div>
  57. ';
  58. }
  59.  
  60. private function totalVotes($answers)
  61. {
  62. //Select the votes from the database for every answer
  63. foreach($answers as $answer)
  64. {
  65. try
  66. {
  67. $stmt = $this->_db->prepare("SELECT votes FROM poll WHERE language = ?");
  68. $stmt->execute(array($answer));
  69. $getvotes[] = $stmt->fetch(PDO::FETCH_ASSOC);
  70. }
  71. catch(PDOException $e)
  72. {
  73. echo $e->getMessage();
  74. }
  75. }
  76.  
  77. //Add the all the votes together
  78. for($i=0; $i < count($getvotes); $i++)
  79. {
  80. $totalvotes += $getvotes[$i][votes];
  81. }
  82.  
  83. return $totalvotes;
  84. }
  85.  
  86. //Display the vote results
  87. public function displayResults()
  88. {
  89. //Get the total amount of votes
  90. $totalvotes = $this->totalVotes($this->answers);
  91.  
  92. //Get the results
  93. $stmt = $this->_db->prepare("SELECT * FROM poll");
  94. $stmt->execute();
  95.  
  96. echo '<div class="poll">';
  97.  
  98. while($results = $stmt->fetch())
  99. {
  100. //calculate the percentage //replace 45 with total votes
  101. $percent = floor(100 / $totalvotes * $results['votes']);
  102.  
  103. echo '<div class="section">
  104. '.$results['language'].': '.$percent.'% with '.$results['votes'].' votes
  105. <div class="bar" style="width:'.$percent.'%;"></div>
  106. </div>
  107. ';
  108. }
  109.  
  110. echo '</div>';
  111. }
  112. }
  113.  
  114. ?>
  115.  
  116.  
  117.  
  118. <?php
  119.  
  120. //To set up and use the Vote Class
  121.  
  122. require_once('Vote.class.php');
  123.  
  124. //Create a new instance and set a question
  125. $vote = new Vote('What is your favorite scripting language?');
  126.  
  127. //Set the answers
  128. $vote->setAnswers('PHP');
  129. $vote->setAnswers('JAVA');
  130. $vote->setAnswers('PERL');
  131. $vote->setAnswers('HTML');
  132.  
  133. //If a user has voted
  134. if(isset($_POST['vote']))
  135. {
  136. $vote->insertVote($_POST['vote']);
  137. $vote->displayResults();
  138. }
  139. //If the user has clicked the Show Results button
  140. else if(isset($_GET['results']))
  141. {
  142. $vote->displayResults();
  143. }
  144. else
  145. {
  146. $vote->displayVote();
  147. }
  148.  
  149. ?>
  150.  
  151.  
  152. The CSS i used.
  153.  
  154. .poll {
  155. width:200px;
  156. padding:10px;
  157. font-size:15px;
  158. background:#F9F9F9;
  159. outline:1px solid #DFDFDF;
  160. border:1px solid #ffffff;
  161. }
  162. .poll .section {
  163. margin-top:10px;
  164. }
  165. .poll .bar {
  166. height:10px;
  167. background:#FA4343; /* Fallback IE, Opera */
  168. background:-moz-linear-gradient(top, #FA4343, #D91111); /* Mozilla: */
  169. background:-webkit-gradient(linear, left top, left bottom, from(#FA4343), to(#D91111)); /* Chrome, Safari:*/
  170. border:1px #D91111 solid;
  171. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.