home *** CD-ROM | disk | FTP | other *** search
Wrap
<?php //a function to give us useful error messages function displayError() { die("Error " . mysql_errno() . " - " . mysql_error()); } //connect to the database if (!($conn = @ mysql_connect("localhost","root","pwd"))) die("Could not connect!"); //choose the poll db if (!(mysql_select_db("dbPoll", $conn))) displayError(); //set a variable to say which poll we are loading $latest = mysql_query("SELECT questionID FROM tblQuestions ORDER BY questionID DESC LIMIT 1", $conn); $thisPoll = mysql_fetch_array($latest); $qID = $thisPoll['questionID']; //if the form has been posted then insert the results into the database if ($_POST['answerID'] != "") { //insert results if (!$HTTP_COOKIE_VARS["voted" . $qID] == $qID) { $sql = "INSERT INTO tblResponse (questionID,answerID) VALUES (" . $_POST['questionID'] . ",". $_POST['answerID'] . ")"; if (!(mysql_query($sql, $conn))) { displayError(); }else { $voted = true; setcookie("voted" .$_POST['questionID'], $_POST['questionID'], time()+31536000); } } } //check to see if the user has a cookie for this poll if ($HTTP_COOKIE_VARS["voted" . $qID] == $qID) { $voted = true; } //query the database for a list of questions if (!($result = @ mysql_query("SELECT * FROM tblQuestions, tblAnswers WHERE tblQuestions.questionID = tblAnswers.questionID AND tblQuestions.questionID = " . $qID, $conn))) displayError(); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Poll</title> </head> <body> <?php if ($voted == true) { $votes = mysql_query("SELECT * FROM tblResponse WHERE questionID = " . $qID); $i = mysql_num_rows($votes); //display results $question = mysql_fetch_array($result); echo "<table summary=\"Results for question " . $question['questionText'] . "\">\n"; echo "<caption>" . $question['questionText'] . "</caption>"; mysql_data_seek($result,0); while ($row = mysql_fetch_array($result)) { $count = mysql_query("SELECT COUNT(answerID) FROM tblResponse WHERE answerID = " . $row['answerID'],$conn); $n = mysql_fetch_array($count); //get the percentage $percent = number_format(($n[0]/$i) * 100, 0, " - ", ''); print "<tr><th>" . $row['answerText'] . "</th><td>(" . $percent . "%)</td><td><img src=\"poll.gif\" alt=\"\" height=\"12\" width=\"" . $percent . "\" /></td></tr>"; } echo "</table>\n"; echo "<p>The total number of votes cast is " . $i . "</p>"; } else { //display form ?> <form method="post" action="<?=$_SERVER['PHP_SELF']?>"> <?php $question = mysql_fetch_array($result); echo "<p><strong>" . $question['questionText'] . "</strong>"; echo "<input type=\"hidden\" name=\"questionID\" value=\"" . $question['questionID'] . "\" />\n"; mysql_data_seek($result,0); while ($row = mysql_fetch_array($result)) { print "<br /><input type=\"radio\" name=\"answerID\" value=\"" . $row['answerID'] . "\" />" . $row['answerText']; } ?> <p><input type="submit" name="btnVote" value="vote!" /></p> </form> <?php //how many people have viewed this survey? $votes = mysql_query("SELECT * FROM tblResponse WHERE questionID = " . $qID); $i = mysql_num_rows($votes); ?> <p><strong><?=$i?></strong> votes have been cast</p> <?php } ?> </body> </html>