<?php

require 'dbconn.php';

$action = (isset($_GET['a'])) ? $_GET['a'] : "";
$pollID = (isset($_GET['id'])) ? $_GET['id'] : "";

if ($action == "getpolldata")
{
   $sql = "SELECT * FROM poll WHERE id=" . $pollID;
   $query = mysql_query($sql);
   $total = 0;

   $xml = "";
   while ($row = mysql_fetch_array($query))
   {
      $xml .= "\t<element";
      $xml .= " type=\"" . $row['pollType'] . "\""; 
      $xml .= " value=\"" . $row['pollValue'] . "\""; 

      if ($row['pollType'] == "answer")
      {
         $xml .= " votes=\"" . $row['pollVotes'] . "\"";
         $total += $row['pollVotes'];
      }

      $xml .= " />\n";
   }
   $xml .= "</poll>";

   $xml = "<poll id=\"" . $pollID . "\" total=\"" . $total . "\">\n" . $xml;

   header("Content-type: text/xml");

   print $xml;
}
else if ($action == "vote")
{
   $votes = (isset($_GET['v'])) ? $_GET['v'] : "";
   $question = (isset($_GET['q'])) ? $_GET['q'] : "";

   $votes = $votes + 1;

   $sql = "UPDATE poll SET pollValue=" . $votes . " WHERE id=" . $pollID . " AND pollValue=" . $question;
   mysql_query($sql);
}

?>