...

View Full Version : PHP Poll not working right



bern72358
12-14-2010, 07:03 PM
I had asked for an advice and was helped but not quite right.
I really need help! Totallly new to php and javascript.

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>

with this 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];

$totalVotes = $no + $yes;
if ($totalVotes <= 0)
{
echo 'There are not votes at this time.';
}
else
{
$yesPercent = 100 * ($yes / $totalVotes);
$noPercent = 100 * ($no / $totalVotes);


//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 $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
}

textfile is this:

0||0

Result that is not quite right:
http://i969.photobucket.com/albums/ae180/bern_031/wrong.png

I want it to show like this:
http://i969.photobucket.com/albums/ae180/bern_031/Decorated%20images/faqscreen.png

Percentage and the number of votes for each choice is what I only need.

Can someone help?
Thanks thanks thanks

surreal5335
12-14-2010, 10:12 PM
The variables that you will want to use is your

$no = number of votes for no
$noPercent = % for no
$yes = number of votes for yes
$yesPercent = % for yes
$totalVotes = total votes for both combined

All of your output for yes and no is done on:
<?php printf("%d/%d (%0.2f%%)", $yes, $totalVotes, $yesPercent); ?>
and
<?php printf("%d/%d (%0.2f%%)", $no, $totalVotes, $noPercent); ?>

change them to this:

<?php echo $yesPercent.' = '.$yes.' votes'; ?>
and
<?php echo $noPercent.' = '.$no.' votes'; ?>

while the printf() is a powerful output formating function, I wouldnt recommend it to beginners, it can be rather confusing. Even working with it, I still dont use it... I just echo out html and css for my formatting needs.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum