Skip to content

Instantly share code, notes, and snippets.

@sameh-sharaf
Created September 12, 2016 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sameh-sharaf/e8430f88402b468d1a56a8c47809e0e5 to your computer and use it in GitHub Desktop.
Save sameh-sharaf/e8430f88402b468d1a56a8c47809e0e5 to your computer and use it in GitHub Desktop.
A poll class written in PHP to manage poll voting and view on page.
<?php
class Vote
{
private $username = "root";
private $password = "";
private $database = "";
private $barWidth = 200;
private $barHeight = 10;
private $showtablewidth = "280";
private $showAddForm = 400;
private $voteBar = "images/poll/bar.jpg";
private $parentTable = "vote";
private $childTable = "votechoice";
public function __construct($username, $password, $database)
{
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
public function connect()
{
$con = new dbConnection($this->username, $this->password, $this->database);
$con->connect();
return $con;
}
public function showVoteMenu()
{
if(!$con = $this->connect())
{
return false;
}
// Show Vote Menu
$query = "SELECT * FROM vote WHERE vote_date IS NULL OR vote_date >= '" . date('Y-M-d') . "'";
$localVote = $con->executeQuery($query);
if(!$localVote)
{
echo "No Votes Available.";
return false;
}
else
{
$rand = rand(0, count($localVote) - 2);
$query = "SELECT votechoice_id, votechoice_string FROM votechoice WHERE votechoice_voteid = " . $localVote[$rand]['vote_id'];
$voteChoices = $con->executeQuery($query);
if(!$voteChoices)
{
return false;
}
else
{
?>
<!-- AJAX -->
<script type="text/javascript">
function getVote(params)
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new window.XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){getResults(xmlhttp);}
xmlhttp.open("POST","includes/poll/vote_submit.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}
function getResults(httpReq)
{
if (httpReq.readyState == 4 && httpReq.status == 200)
{
// Show loading image
document.getElementById("poll").innerHTML = "";
document.getElementById("poll").innerHTML = httpReq.responseText;
}
}
</script>
<!-- jQuery -->
<script type="text/javascript">
$('document').ready(
function()
{
$(':input#submit').click(
function(e)
{
if(!($('#voteForm input[type=checkbox]:checked').length || $('#voteForm input[type=radio]:checked').length))
{
alert("يرجى الاختيار أولاً");
e.preventDefault();
}
else
{
var ajaxParams = "";
var ajaxForm = document.getElementById("voteForm");
// Create params string
for (i = 0;i< ajaxForm.elements.length ; i++)
{
if (((ajaxForm[i].type == 'radio' || ajaxForm[i].type == 'checkbox') && ajaxForm[i].checked == true) || (ajaxForm[i].type != 'radio' && ajaxForm[i].type != 'checkbox'))
{
ajaxParams += ajaxForm[i].name + "=" + ajaxForm[i].value + "&";
}
}
// Omit '&' from end of string
ajaxParams = ajaxParams.substr(0,ajaxParams.length-1);
document.getElementById("voteForm").innerHTML = "<p align='center'><img src='images/poll/loading.gif' /></p>";
getVote(ajaxParams);
}
}
);
$(':input#showResults').click(
function(e)
{
var params = document.getElementById('showResults').name + "=" + document.getElementById('showResults').value + "&" + document.getElementById('vote_id').name + "=" + document.getElementById('vote_id').value;
document.getElementById("voteForm").innerHTML = "<p align='center'><img src='images/poll/loading.gif' /></p>";
getVote(params);
}
);
}
);
</script>
<div id="poll">
<form id="voteForm" method="POST" dir="rtl" onsubmit="return false;">
<table id="pollTable" border="0" align="center" width="<?php echo $this->showtablewidth; ?>">
<tr>
<td colspan="2" class="title">
<h2>
صوّت
</h2>
</td>
</tr>
<tr>
<td colspan="2">
<?php echo $localVote[$rand]['vote_question'];?>
</td>
</tr>
<?php
for($i=0; $i<count($voteChoices) - 1; $i++)
{
echo "<tr>";
echo "<td width='10'>";
if($localVote[$rand]['vote_type'] == "single")
{
echo "<input type='radio' id='voteradio' name='voteradio' value='".$voteChoices[$i]['votechoice_id']."'>";
}
else
{
echo "<input type='checkbox' id='votecheck' name='votecheck[]' value='".$voteChoices[$i]['votechoice_id']."'>";
}
echo "</td>";
echo "<td>";
echo $voteChoices[$i]['votechoice_string'];
echo "</td>";
echo "</tr>";
}
?>
<tr>
<td align="center" colspan="2">
<input type="submit" id="submit" name="submit" value="صوت">
<input type="submit" id="showResults" name="showResults" value="إظهار النتائج">
<input type="hidden" id="vote_id" name="vote_id" value="<?php echo $localVote[$rand]['vote_id']; ?>">
</td>
</tr>
</table>
</form></div>
<div id="loading"></div>
<?php
}
}
return true;
}
public function showVoteResults($voteID, $votecheck, $voteradio)
{
if(!$con = $this->connect())
{
return false;
}
// Show Vote Results
$query = "SELECT * FROM vote WHERE vote_id = " . $voteID;
$localVote = $con->executeQuery($query);
if(!$localVote)
{
return false;
}
else
{
// Update Votes
if(isset($_REQUEST['submit']))
{
if($localVote[0]['vote_type'] == "single")
{
$query = "UPDATE votechoice SET votechoice_count = votechoice_count + 1 WHERE votechoice_id = ".$voteradio;
$con->executeQuery($query);
}
else
{
for($i=0; $i<count($votecheck); $i++)
{
$query = "UPDATE votechoice SET votechoice_count = votechoice_count + 1 WHERE votechoice_id = ".$votecheck[$i];
$con->executeQuery($query);
}
}
}
// Show Results
$query = "SELECT SUM(votechoice_count) FROM votechoice WHERE votechoice_voteid = " . $voteID;
$results = $con->executeQuery($query);
$voteCount = $results[0];
$query = "SELECT votechoice_string, votechoice_count FROM votechoice WHERE votechoice_voteid = " . $voteID;
$voteChoices = $con->executeQuery($query);
if(!$voteChoices)
{
return false;
}
else
{
?>
<table id="pollResults" border="0" width="<?php echo $this->showtablewidth; ?>" align="center" dir="rtl">
<tr>
<td colspan="3" id="pollTitle" class="title">
<h2>
صوّت
</h2>
</td>
</tr>
<tr>
<td colspan="3">
<?php echo $localVote[0]['vote_question']?>
</td>
</tr>
<?php
for($i=0; $i<count($voteChoices) - 1; $i++)
{
echo "<tr><td colspan='3'>".$voteChoices[$i]['votechoice_string']."</td></tr>";
if($voteCount[0] == 0)
{
$width = 0;
}
else
$width = round($this->barWidth * $voteChoices[$i]['votechoice_count'] / $voteCount[0]);
$widthpercent = round($width * 100 / $this->barWidth);
echo "<tr>";
echo "<td width='20'>".$voteChoices[$i]['votechoice_count']."</td>";
echo "<td><img src='".$this->voteBar."' width='".$widthpercent."%' height='".$this->barHeight."'></td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='2'>الأصوات : ".$voteCount[0]."</td>";
echo "</tr>";
?>
</table>
<?php
//mysql_close();
return true;
}
}
}
///////////////////////////////////////////////////////////////
public function showAddNewVoteForm($votes)
{
?>
<!--<script language="JavaScript">
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 'Vote '+num+'<input type=\"text\" name=\"votestrings[]\"><br>';
ni.appendChild(newdiv);
}
</script>
-->
<form action="" method="GET">
<table border="1" align="center" width="<?php echo $this->showAddForm; ?>">
<tr>
<td colspan="2" align="center">
Add New Vote
</td>
</tr>
<tr>
<td>
Vote Title
</td>
<td>
<input type="text" size="20" name="title">
</td>
</tr>
<tr>
<td>
Vote Question
</td>
<td>
<input type="text" name="question" size="40">
</td>
</tr>
<tr>
<td>
Vote Type
</td>
<td>
<input type="radio" name="type" value="single" checked="true"> Single Choices <input type="radio" name="type" value="multiple"> Multiple Choices
</td>
</tr>
<tr>
<td colspan="2">
<div id="myDiv">
Vote 1<input type="text" name="votestrings[]"><br>
Vote 2<input type="text" name="votestrings[]"><br>
Vote 3<input type="text" name="votestrings[]"><br>
<input type="hidden" name="theValue" value="3">
</div>
</td>
</tr>
<tr>
<td colspan="2">
<a href="javascript:;" onclick="var ni = document.getElementById('myDiv'); var numi = document.getElementById('theValue'); var num = (document.getElementById('theValue').value -1)+ 2; numi.value = num; var newdiv = document.createElement('div'); var divIdName = 'my'+num+'Div'; newdiv.setAttribute('id',divIdName); newdiv.innerHTML = 'Vote '+num+'<input type=text name=votestrings[]><br>'; ni.appendChild(newdiv);">Add New Vote</a>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="Add">
<input type="reset" name="reset" value="Reset">
</td>
</tr>
</table>
</form>
<?php
}
public function insertVoteToDB($title, $question, $type, $votestrings)
{
if(!$this->connectDB($this->database))
{
return false;
}
else
{
// Add vote to 'vote' table
$query = "INSERT INTO vote(vote_id, vote_title, vote_question, vote_type, vote_date) VALUES (null, '".$title."','".$question."', '".$type."', '".date('y-m-d')."')";
mysql_query($query);
// Get recently added Vote ID
$query = "SELECT MAX(vote_id) FROM vote";
$result = mysql_query($query);
$result = mysql_fetch_array($result);
$id = $result[0];
// Add vote choices to 'votechoice' table
echo count($votestrings) . "<br>";
for($i=0; $i<count($votestrings); $i++)
{
echo $votestrings['votechoice_string'] . "<br>";
$query = "INSERT INTO votechoice(votechoice_id, votechoice_voteid, votechoice_string, votechoice_count) VALUES (null, $id, '".$votestrings[$i]."', 0)";
mysql_query($query);
}
mysql_close();
return true;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment