
# Table structure for table `poll_data` # CREATE TABLE poll_data ( ID tinyint(4) NOT NULL auto_increment, Option1 tinyint(4) NOT NULL default '0', Option2 tinyint(4) NOT NULL default '0', Option3 tinyint(4) NOT NULL default '0', Votes tinyint(4) NOT NULL default '0', Title varchar(25) NOT NULL default '', Question varchar(50) NOT NULL default '', PRIMARY KEY (ID) ) TYPE=MyISAM;Now that we have the table set up, we want to get coding. Most of the time, when you are going to write a script or program you will want to break it down into small parts. Like:
$sql = "SELECT `Title`,`Question`,`ID` FROM `poll_data` ORDER BY `ID` DESC LIMIT 1"; if(!($result = mysql_query($sql))) die(mysql_error()); $PollData = mysql_fetch_array($resultNow that we have the Title and Question of the poll, we want the all the good stuff, the votes and the percentages. Again we want the newest record, so we use desc order again.
if($VoteData["Option1"] != 0) {
$VotePercent1 = Round(($VoteData["Option1"] / $VoteData["Votes"]) * 100) . "%";
} else {
$VotePercent1 = 0 ."%";
}
if($VoteData["Option2"] != 0) {
$VotePercent2 = Round(($VoteData["Option2"] / $VoteData["Votes"]) * 100) . "%";
} else {
$VotePercent2 = 0 ."%";
}
if($VoteData["Option3"] != 0) {
$VotePercent3 = Round(($VoteData["Option3"] / $VoteData["Votes"]) * 100) . "%";
} else {
$VotePercent3 = 0 ."%";
}
Now that we have all the data we need, we need to display that information in the table. Before you do this, you need to make an image that is 20 pixels high and 1 pixel wide of any color. This part is very self explainatory. We have a table and output the data. We set the image width to be the vote percent. We have a form with radio buttons to let the user pic their vote.
<html>
<head>>
<title>Basic Poll - Written by Adman</title>
</head>
<body>
<form method="POST" action="vote.php">
<table width="500" border="1" cellspacing="0" cellpadding="8">
<tr>
<td colspan="3"><b><?=$PollData['Title']?> - <?=$PollData['Question']?></b></td>
</tr>
<tr>
<td width="35%">
<input type="radio" name="Vote" value="Option1">
Yes</td>
<td width=60%>
<img src="bar.gif" width="<?=$VotePercent1?>" height="20">
</td>
<td><?=$VoteData["Option1"]?> Votes</td>
</tr>
<tr>
<td width="35%">
<input type="radio" name="Vote" value="Option2">
No </td>
<td width=60%>
<img src="bar.gif" width="<?=$VotePercent2?>" height="20">
</td>
<td><?=$VoteData["Option2"]?> Votes</td>
</tr>
<tr>
<td width="35%">
<input type="radio" name="Vote" value="Option3" >
Not Sure</td>
<td width="60%">
<img src="bar.gif" width="<?=$VotePercent3?>" height="20">
</td>
<td><?=$VoteData["Option3"]?> Votes</td>
</tr>
<tr>
<td colspan="3">
<center>
<input type="submit" name="Submit" value="Vote">
</center>
</td>
</tr>
</table>
</form>
</body>
</html>
Now that we have the display done. We are ready to write the script that lets the user vote in the poll. On to the next page!
First we check to see the user actually did vote, if not, exit the script. Since the variable was passed through a post form, we have to use $_POST.
if(empty($_POST["Vote"])) die("You did not enter your vote");
If the script is still running, we need to connect to the database like we did before, and select the current data on the poll. We want to select all the vote data form the newest record. Then turn it into an array.
$dbhost = "localhost"; $dbname = "misc"; $dbuser = "root"; $dbpass = "trigger"; $link_id = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); $sql = "SELECT `Option1`,`Option2`,`Option3`,`Votes`,`ID` FROM `poll_data` ORDER BY `ID` DESC LIMIT 1"; if(!($result = mysql_query($sql))) die(mysql_error()); if(!($PollData = mysql_fetch_array($result))) die(mysql_error());Now that we have a database connection, we need to decided which option to update. We need to add 1 to the current number of votes, and add 1 to the total number of votes. After that has been done, we need to update that row in the database. So we use the update clause to set the new records. Just like before we need to user $_POST becuase the variables we transmitted through a post metod form.
Copyright © 2000-2008 Spoono, LLC. All rights reserved.
Network: Reseller Web Hosting by Spoono Host | Spoonloads | Absolute Cross
Terms of Service | Privacy Policy.