· 10 years ago · Apr 28, 2016, 07:09 PM
1CREATE 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
13class webPoll {
14
15# makes some things more readable later
16const POLL = true;
17const VOTES = false;
18
19# number of pixels for 1% on display bars
20public $scale = 2;
21
22
23public $question = '';
24public $answers = array();
25private $header = '<form class="webPoll" method="post" action="%src%">
26 <input type="hidden" name="QID" value="%qid%" />
27 <h4>%question%</h4>
28 <fieldset><ul>';
29private $center = '';
30private $footer = "n</ul></fieldset>%button%n</form>n";
31private $button = '<p class="buttons"><button type="submit" class="vote">Vote!</button></p>';
32private $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 */
40public 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
50isset($_COOKIE[$this->md5]) ? $this->poll(self::VOTES) : $this- >poll(self::POLL);
51}
52private 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
57if(!$show_poll) {
58 $results = webPoll::getData($this->md5);
59 $votes = array_sum($results);
60}
61
62for( $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
66echo $this->header, $this->center, $this->footer;
67}
68private function pollLine($x) {
69isset($this->answers[$x+1]) ? $class = 'bordered' : $class = '';
70return "
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}
79private function voteLine($answer,$result,$votes) {
80$result = isset($result) ? $result : 0;
81$percent = round(($result/$votes)*100);
82$width = $percent * $this->scale;
83return "
84<li>
85 <div class='result' style='width:{$width}px;'> </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 */
96static function vote() {
97if(!isset($_POST['QID']) ||
98!isset($_POST['AID']) ||
99isset($_COOKIE[$_POST['QID']])) {
100 return;
101}
102try{
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
109try {
110 $sth = $dbh->prepare("INSERT INTO tally (QID,AID,votes) values ('QID', 'AID', '1')" );
111 $sth->execute(array($_POST['QID'],$_POST['AID']));
112}
113catch(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
130if($sth->rowCount() == 1) {
131 setcookie($_POST['QID'], 1, time()+60*60*24*365);
132 $_COOKIE[$_POST['QID']] = 1;
133}
134}
135static function getData($question_id) {
136try {
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}
143catch(PDOException $e) {
144 # Error getting data, just send empty data set
145 webPoll::db_error($e->getMessage());
146 return array(0);
147}
148
149while($row = $STH->fetch()) {
150 $results[$row['AID']] = $row['votes'];
151}
152
153return $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 */
159static function db_error($error) {
160echo "A database error has occured. $error";
161exit;
162}
163}