home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 89 / PIWD89.iso / pc / CONTENTS / DEVELOPER / TUTORIAL_FILES / Pages88-89 / poll.php < prev    next >
Encoding:
PHP Script  |  2003-11-24  |  3.3 KB  |  95 lines

  1. <?php
  2. //a function to give us useful error messages
  3. function displayError()
  4. {
  5.     die("Error " . mysql_errno() . " - " . mysql_error());
  6. }
  7. //connect to the database
  8. if (!($conn = @ mysql_connect("localhost","root","pwd")))
  9. die("Could not connect!");
  10. //choose the poll db
  11. if (!(mysql_select_db("dbPoll", $conn)))
  12. displayError();
  13. //set a variable to say which poll we are loading
  14. $latest = mysql_query("SELECT questionID FROM tblQuestions ORDER BY questionID DESC LIMIT 1", $conn);
  15. $thisPoll = mysql_fetch_array($latest);
  16. $qID = $thisPoll['questionID'];
  17. //if the form has been posted then insert the results into the database
  18. if ($_POST['answerID'] != "") {
  19.     //insert results
  20.     if (!$HTTP_COOKIE_VARS["voted" . $qID] == $qID)
  21.     {
  22.         $sql = "INSERT INTO tblResponse (questionID,answerID) VALUES (" . $_POST['questionID'] . ",". $_POST['answerID'] . ")";
  23.         if (!(mysql_query($sql, $conn)))
  24.         {
  25.             displayError();
  26.         }else {
  27.             $voted = true;
  28.             setcookie("voted" .$_POST['questionID'], $_POST['questionID'], time()+31536000);
  29.         }
  30.     }
  31. }
  32. //check to see if the user has a cookie for this poll
  33.  
  34. if ($HTTP_COOKIE_VARS["voted" . $qID] == $qID) {
  35.     $voted = true;
  36. }
  37. //query the database for a list of questions
  38. if (!($result = @ mysql_query("SELECT * FROM tblQuestions, tblAnswers WHERE tblQuestions.questionID = tblAnswers.questionID AND tblQuestions.questionID = " . $qID, $conn)))
  39. displayError();
  40. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  41.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  42. <html>
  43. <head>
  44. <title>Poll</title>
  45. </head>
  46. <body>
  47. <?php
  48. if ($voted == true) {
  49.     $votes = mysql_query("SELECT * FROM tblResponse WHERE questionID = " . $qID);
  50.     $i = mysql_num_rows($votes);
  51.     
  52.     //display results
  53.     $question = mysql_fetch_array($result);
  54.     echo "<table summary=\"Results for question " . $question['questionText'] . "\">\n";
  55.     echo "<caption>" . $question['questionText'] . "</caption>";
  56.     mysql_data_seek($result,0);
  57.     while ($row = mysql_fetch_array($result)) {
  58.         $count = mysql_query("SELECT COUNT(answerID) FROM tblResponse WHERE answerID = " . $row['answerID'],$conn);
  59.         $n = mysql_fetch_array($count);
  60.         //get the percentage
  61.         $percent = number_format(($n[0]/$i) * 100, 0, " - ", '');
  62.         
  63.         print "<tr><th>" . $row['answerText'] . "</th><td>(" . $percent . "%)</td><td><img src=\"poll.gif\" alt=\"\" height=\"12\" width=\"" . $percent . "\" /></td></tr>";
  64.     }
  65.     echo "</table>\n";
  66.     echo "<p>The total number of votes cast is " . $i . "</p>";
  67.     
  68. } else {
  69.     //display form
  70.     ?>
  71.     <form method="post" action="<?=$_SERVER['PHP_SELF']?>">
  72.     <?php
  73.     $question = mysql_fetch_array($result);
  74.     echo "<p><strong>" . $question['questionText'] . "</strong>";
  75.     echo "<input type=\"hidden\" name=\"questionID\" value=\"" . $question['questionID'] . "\" />\n";
  76.     mysql_data_seek($result,0);
  77.     while ($row = mysql_fetch_array($result)) {
  78.         print "<br /><input type=\"radio\" name=\"answerID\" value=\"" . $row['answerID'] . "\" />" . $row['answerText'];
  79.     }
  80.     ?>
  81.     <p><input type="submit" name="btnVote" value="vote!" /></p>
  82.     </form>
  83. <?php
  84. //how many people have viewed this survey?
  85. $votes = mysql_query("SELECT * FROM tblResponse WHERE questionID = " . $qID);
  86. $i = mysql_num_rows($votes);
  87. ?>
  88. <p><strong><?=$i?></strong> votes have been cast</p>
  89. <?php
  90. }
  91. ?>
  92. </body>
  93. </html>
  94.  
  95.