index.php
codice:
<?php
include ('Poll.php');
$poll = new Poll();
$voted = 0;
$pollData = $poll->getPoll();
if(isset($_POST['vote'])){
$pollVoteData = array(
'sondaggioid' => $_POST['sondaggioid'],
'pollOptions' => $_POST['options']
);
$isVoted = $poll->updateVote($pollVoteData);
if($isVoted){
setcookie($_POST['sondaggioid'], 1, time()+60*60*24*365);
$voted = 1;
} else {
$voted = 2;
}
}
?>
<div class="container">
<div class="row">
<?php if(!empty($voted) && $voted === 1) {
echo '<div class="alert alert-success">Hai votato con successo.</div>';
}
else if(!empty($voted) && $voted === 2) {
echo '<div class="alert alert-danger">Your had already voted.</div>';
}
?>
<form action="" method="post" name="pollForm">
<?php foreach($pollData as $poll){
$pollOptions = explode("||||", $poll['options']);?>
<div class="col-md-3">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
<?php echo $poll['question']?>
</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<?php for( $i = 0; $i < count($pollOptions); $i++ ) { ?>
<li class="list-group-item">
<div class="radio">
<label>
<input type="radio" name="options" value="<?php echo $i; ?>">
<?php echo $pollOptions[$i]?>
</label>
</div>
</li>
<?php }?>
</ul>
</div>
<div class="panel-footer">
<input type="hidden" name="sondaggioid" value="<?php echo $poll['sondaggioid']; ?>"/>
<button type="submit" class="btn btn-primary btn-sm" id="vote" name="vote">
Vote</button>
</div>
</div>
</div>
<?php }?>
</form>
</div>
</div>
</body>
</html>
Poll.php
codice:
<?php
class Poll{
private $host = 'localhost';
private $user = 'root';
private $password = '';
private $database = 'sondaggio';
private $pollTable = 'poll';
private $dbConnect = false;
public function __construct(){
if(!$this->dbConnect){
$conn = new mysqli($this->host, $this->user, $this->password, $this->database);
if($conn->connect_error){
die("Error failed to connect to MySQL: " . $conn->connect_error);
}else{
$this->dbConnect = $conn;
}
}
}
private function getData($sqlQuery) {
$result = mysqli_query($this->dbConnect, $sqlQuery);
if(!$result){
die('Error in query: '. mysqli_error());
}
$data= array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$data[]=$row;
}
return $data;
}
public function getPoll(){
$sqlQuery = 'SELECT sondaggioid, question, options, votes, voters FROM '.$this->pollTable;
return $this->getData($sqlQuery);
}
public function getVotes($sondaggioid){
$sqlQuery = 'SELECT votes, voters FROM '.$this->pollTable.' where sondaggioid = '.$sondaggioid;
$result = mysqli_query($this->dbConnect, $sqlQuery);
return mysqli_fetch_array($result, MYSQLI_ASSOC);
}
public function updateVote($pollVoteData) {
if(!isset($pollVoteData['sondaggioid']) || isset($_COOKIE[$pollVoteData['sondaggioid']])) {
return false;
}
$pollVoteDetails = $this->getVotes($pollVoteData['sondaggioid']);
$votes = explode("||||", $pollVoteDetails['votes']);
$votes[$pollVoteData['pollOptions']] += 1;
implode("||||",$votes);
$pollVoteDetails['voters'] += 1;
$sqlQuery = "UPDATE ".$this->pollTable." set votes = '".implode("||||",$votes)."' , voters = '".$pollVoteDetails['voters']."' where sondaggioid = '".$pollVoteData['sondaggioid']."'";
mysqli_query($this->dbConnect, $sqlQuery);
return true;
}
}
?>