Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Additional option PHP Poll

    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
    Last edited by bern72358; 12-12-2010 at 06:09 PM. Reason: to make it more clear

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
    $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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    No, you pretty much replace this part:
    PHP Code:
    <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?

    Edit:
    Also, I biffed the code above. Where it has the <?php $noPercent;?>, that should be <?php echo $noPercent;?>.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by bern72358; 12-13-2010 at 03:40 PM. Reason: Code worked!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •