poll.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. require_once("./common.php");
  3. $poll_id = $_GET['poll_id'];
  4. // get poll metadata
  5. $query = "SELECT * FROM polls WHERE id = $poll_id";
  6. $result = mysql_query($query);
  7. $poll = mysql_fetch_array($result);
  8. $smarty->assign("maxselect", $poll['maxselect']);
  9. $smarty->assign("title", $poll['title']);
  10. $smarty->assign("description", $poll['description']);
  11. $smarty->assign("poll_id", $poll_id);
  12. // get poll options
  13. $query = "SELECT choice, value FROM poll_options WHERE poll_id = $poll_id";
  14. $result = mysql_query($query);
  15. $num_options = mysql_num_rows($result);
  16. while ($row = mysql_fetch_assoc($result)){
  17. $row['choice'] = urldecode($row['choice']);
  18. $poll_options[] = $row;
  19. }
  20. $smarty->assign("poll_options", $poll_options);
  21. // get poll results
  22. $query = "SELECT name, choice, other FROM results WHERE poll_id = $poll_id";
  23. $result = mysql_query($query);
  24. $totals = array_pad(array(), $num_options, 0);
  25. while ($row = mysql_fetch_assoc($result)){
  26. $name = $row['name'];
  27. $other = urldecode($row['other']);
  28. $value = $row['choice'];
  29. unset($choice);
  30. for ($i = 0; $i < $num_options ; $i++){
  31. if ($choice[$i] = (bool) ($value & (1<<$i)))
  32. $totals[$i]++;;
  33. }
  34. $res['name'] = $name;
  35. $res['other'] = $other;
  36. $res['choice'] = $choice;
  37. $results[] = $res;
  38. }
  39. if (isset($results))
  40. $smarty->assign('results', $results);
  41. $smarty->assign('totals', $totals);
  42. $smarty->display ("poll.html");
  43. ?>