<? /*The example uses the following files:
poll.php3 : the main script
gifdraw.php3 : draws the GIF graph
poll.yn : maintains votes submitted (yes:no)
poll.gif : a blank 200x100 GIF
To download the source go to http://www.campusindia.org/poll/poll.zip
*/
/* poll.php3
*
* Online polling script
* by Sreshth Kumar <skumar@deutech.com>
*
*/ ?>
<HTML><HEAD><TITLE>Online Poll</TITLE></HEAD><BODY>
<H2>Online Poll</H2>
<P>
<? if(isset($cmd) && $cmd=="answered"):
// Show answer
echo "You answered <B>$pollanswer</B>";
echo "<P>The poll results so far:<P>";
// Read from file
$fp=fopen("poll.yn", "r");
$data=fgets($fp, 4096);
$answer = explode(":", $data);
fclose($fp);
// Check answer and update file
if($pollanswer=="YES"): $answer[0]++;
elseif($pollanswer=="NO"): $answer[1]++;
endif;
// Write to file
$fp=fopen("poll.yn", "w");
$data = implode(":", $answer);
fputs($fp, $data);
fclose($fp);
// Show poll result
echo "<P><IMG SRC="gifdraw.php3?$data" BORDER=0><P>";
endif; ?>
<B>Will India send troops to Sri Lanka?</B>
<P>
<FORM NAME="pollform" ACTION="<?echo $PHP_SELF?>" METHOD=POST>
<INPUT TYPE=hidden NAME=cmd VALUE=answered>
<INPUT TYPE=radio NAME=pollanswer VALUE=YES>Yes
<INPUT TYPE=radio NAME=pollanswer VALUE=NO>No
<P>
<INPUT TYPE=submit VALUE=Submit>
</FORM>
</BODY></HTML>
<? /* gifdraw.php3
*
* Online polling script
* GIF drawing module
* by Sreshth Kumar <skumar@deutech.com>
*
*/
$data=$argv[0];
$answer = explode(":", $data);
$yes=$answer[0];
$no =$answer[1];
if($yes>$no): $max=$yes;
else: $max=$no;
endif;
$yes_barlength=round(($yes/$max)*160);
$no_barlength=round(($no/$max)*160);
$im=ImageCreateFromGif("poll.gif");
$font=3;
$red =ImageColorAllocate($im, 255, 0, 0);
$green=ImageColorAllocate($im, 0, 255, 0);
$blue =ImageColorAllocate($im, 0, 0, 255);
$black=ImageColorAllocate($im, 0, 0, 0);
$white=ImageColorAllocate($im, 255, 255, 255);
ImageFilledRectangle($im, 40,30, $yes_barlength,45, $green);
ImageFilledRectangle($im, 40,55, $no_barlength,70, $red);
$yes_textpos=$yes_barlength+10;
$no_textpos=$no_barlength+10;
$total=$yes+$no;
$yes_percent=round(($yes/$total)*100);
$no_percent=round(($no/$total)*100);
$yes_percent="$yes_percent%";
$no_percent="$no_percent%";
$imgX=ImageSX($im);
$yx=($yes_barlength+30)/2;
$nx=($no_barlength+30)/2;
$title="ONLINE POLL RESULTS";
$tx=round(($imgX-7.5*strlen($title))/2);
$legend="$total votes cast";
$lx=round(($imgX-7.5*strlen($legend))/2);
ImageString($im, $font, $tx, 5, $title, $blue);
ImageString($im, $font, 10, 30, "YES", $black);
ImageString($im, $font, 10, 55, "NO", $black);
ImageString($im, $font, $yx, 30, $yes, $white);
ImageString($im, $font, $nx, 55, $no, $white);
ImageString($im, $font, $yes_textpos, 30, $yes_percent, $black);
ImageString($im, $font, $no_textpos, 55, $no_percent, $black);
ImageString($im, $font, $lx, 80, $legend, $black);
Header("Content-type: image/gif");
ImageGif($im);
ImageDestroy($im);
?>
|
|