Location: PHPKode > scripts > Voter > voter/class.vote.php
<?php
/*********************************
VoteAnalyzer
Programmed by : Daniel Thul
E-mail        : hide@address.com
*********************************/
/*SoMe InFoS:
Your visitors can vote for, in this example, their favorite color.
They can chose it and then it will be stored in an extern file called 'vote.txt'
Then the solution of the voting can be showed in numbers or as a graphic*/

class vote{

        var $answer;

      function count_vote(){

               $answer = $this->answer;
        /*You can change the values of the radio buttons and the keys in the array, if you don`t ask for the favorite color
        but maybe for the favorite food or something else.*/
        $number = array('red' => '0', 'yellow' => '1', 'blue' => '2', 'green' => '3');
        if ($answer == "red" || $answer == "yellow" || $answer == "blue" || $answer == "green"){
        //The variable answer is used as the key for the array and each color is matched to a number. This number will be written in the file
        include ('vote.txt');
        $ergebnis = explode("|", $string);

        if ($answer == "red"){
        $ergebnis[0]++;
        }
        else if ($answer == "yellow"){
        $ergebnis[1]++;
        }
        else if ($answer == "blue"){
        $ergebnis[2]++;
        }
        else if ($answer == "green"){
        $ergebnis[3]++;
        }

      $datei = fopen ('vote.txt', 'w');
      $number = '<? $string="'.$ergebnis[0].'|'.$ergebnis[1].'|'.$ergebnis[2].'|'.$ergebnis[3].'";';
      fputs ($datei, $number);
      fclose ($datei);
      }
      else{
      exit();
      }//Ende if

      }//Ende function

      function give_result(){
      include ('vote.txt');
      $result = explode("|", $string);
                   $r0 = $result[0];
                   $r1 = $result[1];
                   $r2 = $result[2];
                   $r3 = $result[3];
      $all = $r0+$r1+$r2+$r3;
      $divide = 100/$all;
      $r0s = $r0*$divide;
      $r1s = $r1*$divide;
      $r2s = $r2*$divide;
      $r3s = $r3*$divide;
      $r0s = round($r0s, "1");
      $r1s = round($r1s, "1");
      $r2s = round($r2s, "1");
      $r3s = round($r3s, "1");
      $solution = "<table align=\"center\"><tr><td>Red:</td><td>".$r0s."%</td><td>(".$r0." votes)</td></tr>";
      $solution .= "<tr><td>Yellow:</td><td>".$r1s."%</td><td>(".$r1." votes)</td></tr>";
      $solution .= "<tr><td>Blue:</td><td>".$r2s."%</td><td>(".$r2." votes)</td></tr>";
      $solution .= "<tr><td>Green:</td><td>".$r3s."%</td><td>(".$r3." votes)</td></tr>";
      return $solution;
      }

            function result_image(){
      include ('vote.txt');
      $result = explode("|", $string);
                $r0 = $result[0];
                $r1 = $result[1];
                $r2 = $result[2];
                $r3 = $result[3];
      $all = $r0+$r1+$r2+$r3;
      $divide = 100/$all;
      $r0s = $r0*$divide*1.8;
      $r1s = $r1*$divide*1.8;
      $r2s = $r2*$divide*1.8;
      $r3s = $r3*$divide*1.8;
      $r0st = $r0*$divide;
      $r1st = $r1*$divide;
      $r2st = $r2*$divide;
      $r3st = $r3*$divide;
      $r0st = round($r0st, "0");
      $r1st = round($r1st, "0");
      $r2st = round($r2st, "0");
      $r3st = round($r3st, "0");
      $r0s = round($r0s, "0");
      $r1s = round($r1s, "0");
      $r2s = round($r2s, "0");
      $r3s = round($r3s, "0");
      $image = ImageCreate(200, 102);
      $grey = ImageColorallocate($image, 200, 200, 200);
      $chart = ImageColorallocate($image, 0, 0, 159);
      $string = ImageColorallocate($image, 255, 0, 0);
              ImageFilledRectangle($image, 10, 11, $r0s, 23, $chart);
              $text = "Red: ".$r0st."% (".$r0." votes)";
              ImageString($image, 4, 20, 10, $text, $string);
      ImageFilledRectangle($image, 10, 34, $r1s, 46, $chart);
      $text = "Yellow: ".$r1st."% (".$r1." votes)";
      ImageString($image, 4, 20, 33, $text, $string);
                          ImageFilledRectangle($image, 10, 57, $r2s, 69, $chart);
                          $text = "Blue: ".$r2st."% (".$r2." votes)";
                          ImageString($image, 4, 20, 56, $text, $string);
      ImageFilledRectangle($image, 10, 80, $r3s, 92, $chart);
      $text = "Green: ".$r3st."% (".$r3." votes)";
      ImageString($image, 4, 20, 79, $text, $string);

      $name = 'vote.jpg';
      ImageJpeg($image, $name, 100);
      ImageDestroy($image);
      return $name;
      }

}//Ende class
?>
Return current item: Voter