...

View Full Version : Additional option PHP Poll



bern72358
12-12-2010, 07:08 PM
I need help to add the number of votes for each choice (not the total votes for the poll) to my poll aside from the percentage.

Here's my JavaScript code:

<script type="text/javascript">
function getVote(int)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("poll").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","poll_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>

And here's my PHP:

<?php
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];

$no = $array[1];

if ($vote == 0)
{
$yes = $yes + 1;
}
if ($vote == 1)
{
$no = $no + 1;
}

//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>

<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>



Please help

Fou-Lu
12-12-2010, 07:52 PM
If I understand what you want, its simply to add like a x/y ratio for the votes out of the total votes?
The first thing that has to change no matter what you do is that $no + $yes has to go. If no votes exist, it will try to divide by 0 which of course is undefined. You know how many exist otherwise.


$totalVotes = $no + $yes;
if ($totalVotes <= 0)
{
// Do something fancier of course.
// You can use the if/else to simply populate if you want and always show the poll regardless of outcome
echo 'There are not votes at this time.';
}
else
{
$yesPercent = 100 * ($yes / $totalVotes);
$noPercent = 100 * ($no / $totalVotes);
?>


<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif"
width='<?php echo $yesPercent; ?>'
height='20'>
<?php printf("%d/%d (%0.2f%%)", $yes, $totalVotes, $yesPercent); ?>
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll.gif"
width='<?php $noPercent; ?>'
height='20'>
<?php printf("%d/%d (%0.2f%%)", $no, $totalVotes, $noPercent); ?>
</td>
</tr>
</table>

<?php
}


Something simple like this what you want?

bern72358
12-13-2010, 03:26 AM
First of all, I want to thank you for a quick reply.
I haven't yet tried your suggestion because I"m so scared that I will be deleting the lines incorrectly:

Are these lines the ones I have to replace?

$array = explode("||", $content[0]);
$yes = $array[0];

$no = $array[1];

if ($vote == 0)
{
$yes = $yes + 1;
}
if ($vote == 1)
{
$no = $no + 1;
}

to these?

$totalVotes = $no + $yes;
if ($totalVotes <= 0)
{
// Do something fancier of course.
// You can use the if/else to simply populate if you want and always show the poll regardless of outcome
echo 'There are not votes at this time.';
}
else
{
$yesPercent = 100 * ($yes / $totalVotes);
$noPercent = 100 * ($no / $totalVotes);
?>


Thank you again and I really do appreciate it...

Fou-Lu
12-13-2010, 05:19 AM
No, you pretty much replace this part:


<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>


With the above. You still need all the previous code.

Simply make a copy before you make a change. I assume you are working on a local copy of these before uploading them to a host right?


Also, I biffed the code above. Where it has the <?php $noPercent;?>, that should be <?php echo $noPercent;?>.

bern72358
12-13-2010, 04:28 PM
All I want is to show the results like this example:

Yes 23% 23 votes
No 77% 50 votes

BUT! Your code worked....

Thanks a ton,
Bern



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum