-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoll.php
More file actions
89 lines (75 loc) · 3.55 KB
/
poll.php
File metadata and controls
89 lines (75 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php @session_start();
$q = array();
parse_str($_SERVER['QUERY_STRING'], $q);
@include('php/_conn.php'); /* require_once: causes an error, include fixes it but causes a warning for already defined constants ... */
//check if user already voted and get choiceid
if(isset($_SESSION['loggedin'])){
$stmt = $pdo->prepare("SELECT choiceid FROM polls_votes WHERE pollid = :pollid AND userid = :userid;");
$stmt->bindValue(":pollid", $q['poll']);
$stmt->bindValue(":userid", $_SESSION['userid']);
$stmt->execute();
if($stmt->rowCount() === 0) $voted = false;
else {
$voted = true;
$choiceid = $stmt->fetchColumn();
}
}
//get authorid, and question from polls
$stmt = $pdo->prepare("SELECT pollid, authorid, question, date FROM polls WHERE pollid = :pollid;");
$stmt->bindValue(":pollid", $q['poll']);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo '<div><h1>'.$row['question'].'</h1>';
//get username from authorid
$stmt = $pdo->prepare("SELECT username FROM users WHERE userid = :authorid;");
$stmt->bindValue(":authorid", $row['authorid']);
$stmt->execute();
echo'<h5>'.$stmt->fetchColumn().'<span style="float:right;">'.$row['date'].'</span></h5></div>';
//get total # of votes
$stmt = $pdo->prepare("SELECT COUNT(userid) FROM polls_votes WHERE pollid = :pollid;");
$stmt->bindValue(":pollid", $q['poll']);
$stmt->execute();
$total = $stmt->fetchColumn();
//get choices from polls_choices
$stmt = $pdo->prepare("SELECT choiceid, choice FROM polls_choices WHERE pollid = :pollid;");
$stmt->bindValue(":pollid", $q['poll']);
$stmt->execute();
echo '<div style="width:98%;margin:0 auto;"><form action="php/poll_vote.php?pollid='.$q['poll'].'" method="post">';
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo '<span>'.$row['choiceid'].') </span>';
if(isset($voted) && !$voted && isset($_SESSION['loggedin'])){
echo '<input type="radio" name="choice" value="'.$row['choiceid'].'"/>';
}
echo '<span> "'.$row['choice'].'"</span>';
//if(!$voted) echo '<br/>';
//else { //user already voted: draw percentages, and a bar graph
$stmt2 = $pdo->prepare("SELECT COUNT(*) FROM polls_votes WHERE pollid = :pollid AND choiceid = :choiceid;");
$stmt2->bindValue(":pollid", $q['poll']);
$stmt2->bindValue(":choiceid", $row['choiceid']);
$stmt2->execute();
$count = $stmt2->fetchColumn();
if($total === 0) $percent = 0;
else $percent = ($count / $total * 100);
echo '<span style="margin-left:15px;"> '.(floor($percent*100)/100).'%</span><span style="margin-left:15px;">('.$count.' votes)</span><div style="background-color:rgb(155,155,155);height:16px;width:';
if($percent <= 0.1) echo '0.1';
else echo $percent;
echo '%;"></div>';
//}
}
echo '<br/><span>'.$total.' total votes</span>';
if(isset($voted) && $voted){
//get choice from choiceid
$stmt = $pdo->prepare("SELECT choice FROM polls_choices WHERE pollid = :pollid AND choiceid = :choiceid;");
$stmt->bindValue(":pollid", $q['poll']);
$stmt->bindValue(":choiceid", $choiceid);
$stmt->execute();
echo '<br/><span>You voted #'.$choiceid.' "'.$stmt->fetchColumn().'".</span>';
}
if(isset($voted) && !$voted && isset($_SESSION['loggedin'])) echo '<br/><button>Vote</button></form><br/>';
else echo '</form>';
if(!isset($_SESSION['loggedin'])) echo '<p style="float:right;padding:0;"><a href="index.php?page=login">Sign in to vote.</a></p>';
echo '</div>';
end:
$pdo = null;
$stmt = null;
?>