Advertisement
Guest User

Untitled

a guest
Jun 24th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.   $votefor = $_REQUEST["votefor"];
  3.  
  4.   // Returns a random RGB color (used to color the vote bars)
  5.   function getRandomColor()
  6.   {
  7.        $r = rand(128,255);
  8.        $g = rand(128,255);
  9.        $b = rand(128,255);
  10.        $color = dechex($r) . dechex($g) . dechex($b);
  11.        echo "$color";
  12.   }
  13.   //Get the IP of the user
  14.   $domain = $_SERVER["REMOTE_ADDR"];
  15.   $today = date("m/d/Y");
  16.  
  17.   echo "<table id=\"tblResults\" align=\"center\">";
  18.  
  19.   //If votefor is null, then we're just viewing results, so don't log the IP
  20.   if ($votefor != "")
  21.   {
  22.  
  23.     //Load the addresses XML file
  24.     $doc = new DOMDocument();
  25.     $doc->load("xml/addresses.xml");
  26.     $addresses = $doc->getElementsByTagName("address");
  27.     $pVoted = false;
  28.     $pFound = false;
  29.  
  30.  
  31.     if ($pFound == false) //Add new node for IP and date to addresses.xml
  32.     {
  33.          echo "<tr><td colspan=\"3\" class=\"message\">Thanks for voting for $votefor! You can vote again tomorrow.</td></tr>";
  34.  
  35.          $newAddy = $doc->getElementsByTagName('addresses')->item(0);
  36.          $newAddressElement = $doc->createElement('address');
  37.  
  38.          $newLastVisitElement = $doc->createElement('lastvisit');
  39.          $newAddressElement->appendChild($newLastVisitElement);
  40.          $newIPElement = $doc->createElement('ip');
  41.          $newAddressElement->appendChild($newIPElement);
  42.  
  43.          $dayvalue = $doc->createTextNode($today);
  44.          $dayvalue = $newLastVisitElement->appendChild($dayvalue);
  45.  
  46.          $ipvalue = $doc->createTextNode($domain);
  47.          $ipvalue = $newIPElement->appendChild($ipvalue);
  48.  
  49.          $newAddy->appendChild($newAddressElement);
  50.  
  51.          $doc->save("xml/addresses.xml");
  52.     }
  53.     else
  54.     {
  55.          echo "<tr><td colspan=\"3\" class=\"message\">Thanks for voting for $votefor!</td></tr>";
  56.     }
  57.     // Update the vote
  58.     $doc = new DOMDocument();
  59.     $doc->load("xml/results.xml");
  60.     $pollitems = $doc->getElementsByTagName("pollitem");
  61.     foreach( $pollitems as $pollitem )
  62.     {
  63.         $entries = $pollitem->getElementsByTagName("entryname");
  64.         $entry = $entries->item(0)->nodeValue;
  65.         if ($entry == $votefor)
  66.         {
  67.              $votes = $pollitem->getElementsByTagName("votes");
  68.              $count = $votes->item(0)->nodeValue;
  69.              $votes->item(0)->nodeValue = $count + 1;
  70.              break;
  71.         }
  72.     }
  73.     $doc->save("xml/results.xml");
  74.     }
  75.   }
  76.   else
  77.   {
  78.      echo "<tr><td colspan=\"3\" class=\"message\">Poll Results</td></tr>";
  79.   }
  80.  
  81.   // Get max vote count
  82.   $doc = new DOMDocument();
  83.   $doc->load("xml/results.xml");
  84.   $maxvotes = 0;
  85.   $pollitems = $doc->getElementsByTagName("pollitem");
  86.   foreach( $pollitems as $pollitem )
  87.   {
  88.     $votes = $pollitem->getElementsByTagName("votes");
  89.     $vote = $votes->item(0)->nodeValue;
  90.     $maxvotes = $maxvotes + $vote;
  91.   }
  92.   // Generate the results table
  93.   $doc = new DOMDocument();
  94.   $doc->load("xml/results.xml");
  95.   $pollitems = $doc->getElementsByTagName("pollitem");
  96.   foreach( $pollitems as $pollitem )
  97.   {
  98.     $entries = $pollitem->getElementsByTagName("entryname");
  99.     $entry = $entries->item(0)->nodeValue;
  100.     $votes = $pollitem->getElementsByTagName("votes");
  101.     $vote = $votes->item(0)->nodeValue;
  102.     $tempWidth = $vote / $maxvotes;
  103.     $tempWidth = 300 * $tempWidth;
  104.     $votepct = round(($vote / $maxvotes) * 100);
  105.     echo "<tr><td width=\"30%\" class=\"polls\">$entry</td>";
  106.     echo "<td width=\"50%\" class=\"resultbar\"><div class=\"bar\" style=\"background-color: ";
  107.         getRandomColor();
  108.         echo "; width: $tempWidth px;\">$votepct%</div></td><td width=\"20%\">($vote votes)</td></tr>";
  109.   }
  110.   echo "<tr><td class=\"total\" colspan=\"3\">$maxvotes people have voted in this poll.</td>";
  111.   echo "</table>";
  112. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement