vote_form.php
<?php
/* Display a vote form. */
require_once("vote_config.php");
$poll = $_GET['poll'];
if (!is_numeric($poll)) {
die("Invalid poll");
}
/* Look up the poll in the database. */
$sql = "SELECT P.question, A.answer, A.answer_ID
FROM poll P, answer A
WHERE P.ID = $poll
AND A.ID = P.ID";
$result = mysql_query($sql, $db) or die ("mysql error: " . mysql_error());
if (mysql_num_rows($result) == 0) {
die('Invalid poll.');
}
/* If the user has already voted, show the results. */
if ($_COOKIE["poll_voted_$poll"]) {
header("Location: vote_tally.php?poll=$poll");
exit;
}
/* Vote form */
$question_list = "";
while($row = mysql_fetch_array($result)) {
$question = $row['question'];
$question_list .= '<li><input name="answer" type="radio" value="' . $row['answer_ID'] . '"> ' . $row['answer'] . '</li>';
}
?>
<html>
<head></head>
<body>
<span style="font-size: 12px;">
<span style="font-weight: bold; font-size: 14px;">
Poll #<?php print $poll; ?>
</span><br />
<span style="font-weight: bold"><?php print $question; ?></span>
<from action="vote_process.php" method="post">
<ul style="list-style-type: none;">
<?php print $question_list; ?>
</ul>
<input name="poll" type="hidden" value="<?php print $poll; ?>">
<input name="" type="submit" value="Vote!">
</form>
</span>
</body></html>
vote_tally.php
<?php
/* Display the results of a poll. */
require_once("vote_config.php");
$poll = $_REQUEST['poll'];
if (!is_numeric($poll)) {
die("Invalid poll");
}
/* Look up the question. */
$sql = "SELECT question
FROM poll
WHERE ID = $poll";
$result = @mysql_query($sql, $db) or die ("mysql error: " . mysql_error());
if (mysql_num_rows($result) != 1) {
die('Invalid poll.');
}
$row = mysql_fetch_array($result);
$question = $row["question"];
$query = "SELECT count(*) AS num_total_votes
FROM vote V
WHERE V.ID = $poll";
$result = @mysql_query($query, $db) or die ("mysql error: " . mysql_error());
$row = mysql_fetch_array($result);
$num_total_votes = $row["num_total_votes"];
$query = "SELECT A.answer, A.answer_ID, count(V.answer_ID) as num_votes
FROM answer A
LEFT JOIN vote V
ON V.ID = A.ID
AND V.answer_ID = A.answer_ID
WHERE A.ID = $poll
GROUP BY A.answer_ID
ORDER BY num_votes DESC, A.answer ASC
";
$result = @mysql_query($query, $db) or die ("mysql error: " . mysql_error());
print "<html><head><title>Poll: $question</title></head><body>";
print '<ul style="list-style-type: none; font-size: 12px;">';
print '<li style="font-weight: bold; padding-bottom: 10px;">';
print "Poll #$poll: $question";
print '</li>';
while ($row = mysql_fetch_array($result)) {
if ($num_total_votes !=0) {
$pct = sprintf("%.2f", 100.0 * $row["num_votes"] / $num_total_votes);
} else {
$pct = "0";
}
$boxwidth = strval(1 + intval($pct)) . "px";
print '<li style="clear: left;">';
print "$row[answer]";
print "</li>";
print '<li style="clear: left; padding-bottom: 7px;">';
print '<div style="width: ' . $boxwidth . '; height: 15px;' . '; background: black; margin-right: 5px; float: left;">' . "</div>$pct%";
print '</li>';
}
print '<li style="clear: left;">';
print "Total Votes: $num_total_votes";
print '</li>';
print '</ul>';
print '</body></html>';
?>
vote_process.php
<?php
require_once("vote_config.php");
$poll = $_POST['poll'];
$answer = $_POST['answer'];
if (!is_numeric($poll )|| !is_numeric($answer)) {
die("Invalid poll or answer");
}
/* Look up the poll and answer. */
$sql = "SELECT A.answer_ID
FROM poll P, answer A
WHERE P.ID, = A.ID
AND P.ID = $poll
AND A.answer_ID = $answer";
$result = @mysql_query($sql, $db) or die (mysql_error());
if (mysql_num_rows($result) == 0) {
die('Invalid poll or answer.');
}
/* Check for prior votes. */
if (!$_COOKIE["poll_voted_$poll"]) {
/* Insert the vote. */
$sql = "INSERT INTO `vote` (`answer_ID` , `ID`)
VALUES ($answer, $poll);";
$result = @mysql_query($sql, $db) or
die ("Couldn't insert: " . mysql_error());
/* Mark the poll as voted. */
setcookie("poll_voted_$poll", "1", time() + (60*60*24 * 30));
}
/* Redirect to poll results. */
header("Location: vote_tally.php?poll=$poll");
?>
vote_config.php
<?php
$db = @mysql_connect("xx.xx.xx.xx", "xzeroadds", "xxxxxxx") or
die("Couldn't connect.");
@mysql_select_db("xzeroadds", $db) or die("Couldn't select database.");
?>
And finally the include files for the database
CREATE TABLE `poll` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`question` MEDIUMTEXT NOT NULL ,
PRIMARY KEY ( `ID` )
) TYPE = MYISAM;
CREATE TABLE `answer` (
`answer_ID` INT NOT NULL AUTO_INCREMENT ,
`ID` INT NOT NULL ,
`answer` MEDIUMTEXT NOT NULL ,
PRIMARY KEY (`answer_ID` )
) TYPE = MYISAM;
CREATE TABLE `vote` (
`ID` INT NOT NULL ,
`answer_ID` INT NOT NULL ,
INDEX (`ID`)
) TYPE = MYISAM;
INSERT INTO poll (question) VALUES ("What do you think of the site?");
INSERT INTO answer (ID, answer) VALUES (1, "This site is great!");
INSERT INTO answer (ID, answer) VALUES (1, "This site is ok.");
INSERT INTO answer (ID, answer) VALUES (1, "I'm not going to use this site.");

New Topic/Question
Reply




MultiQuote




|