my first shot at my own php (sort of)... making a poll

Discussion in 'Web Design and Development (archive)' started by brianellisrules, Jan 21, 2004.

  1. macrumors regular

    Joined:
    Oct 17, 2003
    #1
    OK, I'm trying to make a poll. I want it to check a database of IPs so a person can only vote once. I want to have a results file that it reads in, displays and updates based on the person's vote.

    Here's what I have so far:
    PHP:
    <?php

    $pollname 
    wb;

    //get the IP address
    $ip getenv("REMOTE_ADDR");

    //connect to the database
    $db mysql_connect("localhost""brian_vote""password here"); 
    mysql_select_db("brian_vote"$db);

    //check to see if the IP exists
    $grab="select * from winterbeard where ipaddy='$ip'";
    $ipresult=mysql_query($grab) or die("select  fails");
    $ipstatus=mysql_num_rows($ipresult);

    //access the results
    $filename $pollname "results.txt";
    $inf fopen($filename"r");
    $results fread($inffilesize($filename));

    //extract the info
    while(!feof($inf))
    {
    $results .= fgets($inf1024);
    }

    fclose($inf);

    //separate the info into yes/no
    $votes explode("\r\n"$results);

    $yes $votes[0];
    $no $votes[1];

    //display current results (testing purposes)
    if (!$vote) {
    echo (
    "yes: $yes");
    echo (
    "<br>");
    echo (
    "no: $no");
    }

    //if the ip doesn't exist, display the poll options
    if ($ipstatus==0)
    {
    //poll html
    echo ("<p>
    <form method=\"POST\" name=\"winterbeardvote\" action=\"
    $PHP_SELF\"> \n
    <input type=\"radio\" name=\"vote\" value=\"yes\">Yes!<br> \n
    <input type=\"radio\" name=\"vote\" value=\"no\">No!<br> \n
    <input type=\"submit\" VALUE=\"Vote!\"> \n
    </p>"
    );

    //when the person votes update the results
    if ($vote)
    {

    //update the results
    if ($vote=="yes")
    {
    $yes++;
    }

    else
    {
    $no++;
    }

    //update the file (all errors/warnings when I try it)
    $outf=fopen($filename"w+");
    fwrite($outf$yes$no);
    fclose($outf);

    //add the ip to prevent multiple votes
    $addy"insert into winterbeard (ipaddy) values ('$ip')";
    $insert mysql_query($addy);

    //display new results
    echo ("<br>");
    echo (
    "yes: $yes");
    echo (
    "<br>");
    echo (
    "no: $no");
    echo (
    "<p>Thanks for voting!</p>");

    }

    }

    //if the ip address does exist, don't display the poll
    else
    {
    echo (
    "yes: $yes");
    echo (
    "<br>");
    echo (
    "no: $no");
    echo(
    "<p>Thanks for voting!</p>");
    }

    ?>
    I've commented a lot of it so hopefully that'll help. I feel like 90% of it works because:

    1) it reads the current results in from the wbresults.txt file.

    2) it checks the mysql database for the users ip address

    3) if the ip is not present, it allows the person to vote and it increments the count

    4) if the person has voted (i.e. the ip address is present in the database) it displays the results, but won't let them vote.


    the problem: the 3 statements involving the updating of the wbresults.txt file.

    you can try it out if you want, but warning, YOU'LL ONLY GET ONE SHOT! :)

    http://www.brianellisrules.com/temp/vote.inc - the code I included in this message

    http://www.brianellisrules.com/temp/wbresults.txt - the results file

    moved - the script (with my default page header). If you go here, it'll allow you to vote, but once you do vote, 3 warnings pop up (and it increments the count/results). if you hit reload, the warnings go away and the old count (from the file) is displayed (because the ip address exists and it doesn't allow the person the option of voting).

    This is long. My problem: writing to a file. I know, it's basic... but I've fought with this thing on and off all day and I figured I'd put this up before I went to sleep.

    Thanks, youn's are the best.
     
  2. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #2
    Any reason not to put the vote alongside the IP in the MySQL DB? If it was me I'd keep all the collected data in one place...

    Other than that...make sure the permissions on the text file are set to be world writable (usually 777).
     
  3. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #3
    Hmmm, so each IP address would have the vote counted next to it... intriguing. ;) Then I'd just have to have the script add up the votes in each column and display those...

    I'll look into that... and to be honest, I thought about doing that at first, but for whatever reason opted to go to the text file (probably just because I figured it'd be easier to have one number rather than add a column of numbers).
     
  4. macrumors 6502

    Joined:
    Nov 26, 2003
    #4
    "Real" programmers almost never keep an accumulated total of anything. It's more reliable to add up the column when you want to display the total.

    Keeping a running total is dicey. When is the latest value added to the total, what if 2+ values are added at the same time, which one gets added first - can that affect decision making. What if a new value is added to the total and then the rest of the transaction fails for some reason and the total is not re-adjusted.....

    I could go on. Bottom line, add up the column when you want to display the total.

    m
     
  5. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #5
    done and done. sort of.

    right now I just have it record a 'yes' or 'no' in a column next to the ip address. it adds it up and displays the number of yes's and no's correctly.

    now I just need to figure out how to get rid of the voting options once a vote has been cast.... babysteps.

    edit: here's my new code:
    PHP:
    <?php

    //get the IP address
    $ip getenv("REMOTE_ADDR");

    //connect to the database
    $db mysql_connect("localhost""brian_vote""password here"); 
    mysql_select_db("brian_vote"$db);

    //check to see if the IP exists
    $grabip="select * from winterbeard where ipaddy='$ip'";
    $ipresult=mysql_query($grabip) or die("select  fails");
    $ipstatus=mysql_num_rows($ipresult);

    //if the ip doesn't exist, display the poll options
    if ($ipstatus==0)
    {

    //poll html
    echo ("<p>
    <form method=\"POST\"  name=\"winterbeardvote\" action=\"
    $PHP_SELF\"> \n
    <input type=\"radio\" name=\"vote\" value=\"yes\">Yes!<br> \n
    <input type=\"radio\" name=\"vote\" value=\"no\">No!<br> \n
    <input type=\"submit\" VALUE=\"Vote!\"> \n
    </p>"
    );

    //when the person votes update the results
    if ($vote)
    {
    //add the ip to prevent multiple votes
    $info"insert into winterbeard (vote, ipaddy) values ('$vote','$ip')";
    $insertinfo mysql_query($info);

    //check vote numbers from database
    $grabyes="select * from winterbeard where vote='yes'";
    $yesresult=mysql_query($grabyes);
    $yes=mysql_num_rows($yesresult);

    $grabno="select * from winterbeard where vote='no'";
    $noresult=mysql_query($grabno);
    $no=mysql_num_rows($noresult);

    $totalvotes=$yes+$no;
    $yespercent=$yes/$totalvotes*100;
    $nopercent=100-$yespercent;

    //display the results
    echo ("yes: $yespercent% ($yes votes)");
    echo (
    "<br>");
    echo (
    "no: $nopercent% ($no votes)");
    echo (
    "<p>Thanks for voting!</p>");

    }
    }

    //if the ip address does exist, don't display the poll options
    else
    {
    //check vote numbers from database
    $grabyes="select * from winterbeard where vote='yes'";
    $yesresult=mysql_query($grabyes);
    $yes=mysql_num_rows($yesresult);

    $grabno="select * from winterbeard where vote='no'";
    $noresult=mysql_query($grabno);
    $no=mysql_num_rows($noresult);

    $totalvotes=$yes+$no;
    $yespercent=$yes/$totalvotes*100;
    $nopercent=100-$yespercent;

    //display the results
    echo ("yes: $yespercent% ($yes votes)");
    echo (
    "<br>");
    echo (
    "no: $nopercent% ($no votes)");
    echo (
    "<p>Thanks for voting!</p>");
    }

    ?>
    still trying to figure out how to get rid of the option to vote once a vote has been cast... any ideas?
     
  6. macrumors member

    Joined:
    Jan 12, 2004
    Location:
    Minnesota
    #6
    Here is a more thorough IP check...

    PHP:
    <?
        if(
    getenv("HTTP_CLIENT_IP")) { 
        
            
    $ip getenv("HTTP_CLIENT_IP"); 
        
        } elseif(
    getenv("HTTP_X_FORWARDED_FOR")) { 
        
            
    $ip getenv("HTTP_X_FORWARDED_FOR"); 
        
        } else { 
            
            
    $ip getenv("REMOTE_ADDR"); 
        
        }
    ?>
     
  7. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #7
    Hey, thanks for the response. I added that snippet to my code... but, that's not my problem (I should've been more clear, sorry). Hopefully this will help.


    Before (first page load, no vote has been cast):
    [​IMG]

    During (option has been selected, vote has been cast. you can see the results are updated below the voting options, yet the voting options remain.)
    [​IMG]

    After (page reloaded after a vote had been previously cast. everything appears correctly.)
    [​IMG]


    I hope that helps clear things up. Basically, I'd like to have the voting options not displayed in the second picture (after an initial vote has been cast, but the page hasnt' been reloaded).
     
  8. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #8
    Ignore this post, for I am a php-guru.





    I will offer up my coding services at a mere $500/hour.

    I know, it's a deal... consider this a discount for friends & family! ;)
     
  9. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
  10. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #10
    soon enough... just working a little css magic to pretty it up now.
     
  11. macrumors member

    Joined:
    Jan 12, 2004
    Location:
    Minnesota
    #11
    Looks like you figured it out, but this is how I do it:


    PHP:
    <?
    if (
    $_SERVER[REQUEST_METHOD] == "POST") {

    //process the vote and display overall results




    } else {

    // display poll options


    }
    ?>
     
  12. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #12
    moved



    also, arson... I think your way is a lot more efficient and simplier than what I have.
     
  13. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA

Share This Page