Page 1 of 1

Display poll results in percent %

Posted: Mon Jan 28, 2008 3:47 am
by Sindarin
I have created a voting script which takes input from a form and stores it in separate text files. Now, the client needs the results to be displayed by percent (%) and possible percentage bars. How can I make it so?

This is the vote.php script which is called when we submit our preference to the poll:

Code: Select all

<?
/*
 
Vote - Poll Script
 
*/
 
//script variables
$vote_ip_file="results/iplist.txt";
 
//read file and check the IPs from the array
 
$vote_ip_id=fopen($vote_ip_file,"r");
$vote_ip_array=fread($vote_ip_id,9999);
fclose($vote_ip_id);
 
$vote_ip_array = explode("\n", $vote_ip_array);
 
if(in_array($_SERVER['REMOTE_ADDR'], $vote_ip_array)) {
 
// already voted - do not allow to vote again
header("content-type:text/html;charset=utf-8");
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
echo "<style type='text/css'>
<!--
body {
    background-image: url();
    background-color: #F2FBD0;
    margin-left: 7px;
    margin-top: 5px;
}
body,td,th {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
}
</style>";
echo "<br />You have already voted...<br>Thank you.";
echo "<meta http-equiv='refresh' content='5;url=index.html'>";
}
 else {
 
//allow to vote
 
//get the variables from the form
$vote_choice=$_POST['vote_choices'];
 
//check the selection made and if text file exists, store the data
 
//START of choice
 
//get current count - read file
$vote_store_file1_id=fopen("results/".$vote_choice.".txt","r");
$vote_count=fread($vote_store_file1_id,4);
fclose($vote_store_file1_id);
 
//increment by 1
$vote_count=$vote_count+1;
 
//store the data - write file
$vote_store_file1_id=fopen("results/".$vote_choice.".txt","w");
fwrite($vote_store_file1_id,$vote_count);
fclose($vote_store_file1_id);
 
//store ip - write file
$vote_ip_id=fopen($vote_ip_file,"a");
fwrite($vote_ip_id, $_SERVER['REMOTE_ADDR'] . "\n");
fclose($vote_ip_id);
header("content-type:text/html;charset=utf-8");
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
echo "<style type='text/css'>
<!--
body {
    background-image: url();
    background-color: #F2FBD0;
    margin-left: 7px;
    margin-top: 5px;
}
body,td,th {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
}
</style>";
echo "<br />Submitting Vote...";
echo "<meta http-equiv='refresh' content='2;url=results.php'>";
 
//END of choice
 
 
}
?>
This is the readfiles.php which is included in results.php so it can fetch and display variables based on the txt files

Code: Select all

<?
 
//read and output the stored amount from the text files
 
//read file
$vote_file=fopen("results/1.txt","r");
$vote_count1=fread($vote_file,4);
fclose($vote_file);
 
//read file
$vote_file=fopen("results/2.txt","r");
$vote_count2=fread($vote_file,4);
fclose($vote_file);
 
//read file
$vote_file=fopen("results/3.txt","r");
$vote_count3=fread($vote_file,4);
fclose($vote_file);
 
//read file
$vote_file=fopen("results/4.txt","r");
$vote_count4=fread($vote_file,4);
fclose($vote_file);
 
//read file
$vote_file=fopen("results/5.txt","r");
$vote_count5=fread($vote_file,4);
fclose($vote_file);
 
//read file
$vote_file=fopen("results/6.txt","r");
$vote_count6=fread($vote_file,4);
fclose($vote_file);
 
?>

This is the results.php which is where the voting results are shown:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<? 
//read the text files
include_once('readfiles.php'); 
echo "<meta http-equiv='refresh' content='2;url=results.php'>";
?>
<title>Vote</title>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="-1" />
<style type="text/css">
<!--
body {
    background-image: url();
    background-color: #F2FBD0;
    margin-left: 7px;
    margin-top: 5px;
}
body,td,th {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
}
a {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    color: #009933;
}
a:link {
    text-decoration: none;
}
a:visited {
    text-decoration: none;
    color: #009933;
}
a:hover {
    text-decoration: underline;
    color: #009933;
}
a:active {
    text-decoration: none;
    color: #009933;
}
-->
</style></head>
 
<body>
<table width="137" height="161" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="39" align="left" valign="top">Which is the perfect age for a woman?
    When she is:<br /></td>
  </tr>
  <tr>
    <td width="137" height="109" align="left" valign="top"><form id="form1" name="form1" method="post" action="vote.php">
        <br />
      <div align="left">
        19 yrs old<b><? echo "$vote_count1"; ?></b><br />
        <br />
        </div>
      <div align="left">
          20-29 yrs old : <b><? echo "$vote_count2"; ?></b><br />
          <br />
        </div>
      <div align="left">
          30-39 yrs old : <b><? echo "$vote_count3"; ?></b><br />
          <br />
        </div>
      <div align="left">
          40-49 yrs old : <b><? echo "$vote_count4"; ?></b><br />
          <br />
        </div>
      <div align="left">
          50-59 yrs old : <b><? echo "$vote_count5"; ?></b><br />
          <br />
        </div>
                  <div align="left">
            over 60 yrs old : <b><? echo "$vote_count6"; ?></b><br />
            <br />
        </div>
        <div align="center"><br />
          </div>
              </div>
    </form></td>
  </tr>
</table>
<p>  <br />
</p>
</body>
</html>
 
 
p.s. I know this might be more efficient if I stored the results in xml or easier if I had the results in a database but the client won't pay for database :D

Re: Display poll results in percent %

Posted: Mon Jan 28, 2008 4:32 am
by Chris Corbyn
Calculating a percentage is just a matter of getting the fraction, then multiplying by 100.

If you have 3 poll options with the following results:

Red = 56
Green = 5
Blue = 78

Then you add all the numbers together to get the actual number of voters:

56 + 5 + 78 = 139

Then you just divide each by that number, and multiply by 100.

Red = (56 / 139) * 100 = 40.3%
Green = (5 / 139) * 100 = 3.6%
Blue = (78 / 139) * 100 = 56.1%

Re: Display poll results in percent %

Posted: Mon Jan 28, 2008 5:24 am
by Sindarin
Thanks, I was never good at math. :D

Re: Display poll results in percent %

Posted: Tue Jan 29, 2008 7:48 am
by Sindarin
Ahhh! I have another problem though, some numbers now display as 36.3636363636% instead of just 36%. Is there any way to fix that?

Re: Display poll results in percent %

Posted: Tue Jan 29, 2008 8:09 am
by Ollie Saunders
Either use round(), printf() or ini_set('precision', $decmialPlacesHere). round() is probably your best bet.

Re: Display poll results in percent %

Posted: Tue Jan 29, 2008 8:14 am
by Sindarin
ah doesn't floor() do the same?

Re: Display poll results in percent %

Posted: Tue Jan 29, 2008 8:30 am
by Ollie Saunders
not quite

Code: Select all

floor(3.8); // 3
round(3.8); // 4