Zend - The PHP Company




Graphs

Add Code


Online Poll  

Type: code fragment
Added by: deutech
Entered: 29/05/2000
Last modified: 08/12/1999
Rating: - (fewer than 3 votes)
Views: 12529
Generates a bar graph view of the result of an online poll.


<?
/*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($fp4096);
    
$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($im25500);
    
$green=ImageColorAllocate($im02550);
    
$blue =ImageColorAllocate($im00255);
    
$black=ImageColorAllocate($im000);
    
$white=ImageColorAllocate($im255255255);

    
ImageFilledRectangle($im40,30$yes_barlength,45$green);
    
ImageFilledRectangle($im40,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$tx5$title$blue);
    
ImageString($im$font1030"YES"$black);
    
ImageString($im$font1055"NO"$black);
    
ImageString($im$font$yx30$yes$white);
    
ImageString($im$font$nx55$no$white);
    
ImageString($im$font$yes_textpos30$yes_percent$black);
    
ImageString($im$font$no_textpos55$no_percent$black);
    
ImageString($im$font$lx80$legend$black);

    
Header("Content-type: image/gif");
    
ImageGif($im);
    
ImageDestroy($im);

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories