A simple system for creating multiple-choice questions for an audience to answer on their phones. Responses are tallied and displayed in real-time. https://openquestion.netsyms.com

getresults.php 922B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. require_once __DIR__ . "/required.php";
  3. header('Content-Type: application/json');
  4. $qid = $VARS['qid'];
  5. if (!$database->has('questions', ['qid' => $qid])) {
  6. die();
  7. }
  8. $total = $database->count('responses', ['qid' => $qid]);
  9. $answers = $database->select('answers', ['aid', 'atext'], ['qid' => $qid]);
  10. $data['total'] = $total;
  11. $data['answers'] = [];
  12. foreach ($answers as $a) {
  13. $votes = $database->count('responses', ['aid' => $a['aid']]);
  14. $data['answers'][] = [
  15. "id" => $a['aid'],
  16. "votes" => $votes,
  17. "percent" => ($votes / $total) * 100.0 . "%"
  18. ];
  19. }
  20. $responses = $database->select('responses', ['name', 'aid'], ['qid' => $qid]);
  21. $data['responses'] = [];
  22. foreach ($responses as $r) {
  23. if ($r['name'] == null) {
  24. $r['name'] = "Anonymous";
  25. }
  26. $data['responses'][] = [
  27. "name" => $r['name'],
  28. "aid" => $r['aid']
  29. ];
  30. }
  31. echo json_encode($data);