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

    Unhappy PHP Poll not working right

    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:


    I want it to show like this:


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

    Can someone help?
    Thanks thanks thanks

  • #2
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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.


  •  

    Posting Permissions

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