A simple PHP voting program source Code _php Tutorial

Source: Internet
Author: User
Tags string back
Analysis:
We use a file (Data.dat) to store voting columns. Each column occupies one row.
This way, you can add and subtract the columns you want to vote for.
We then use a file (Votes.dat) to store our poll results.
And record the latest voter's IP address, simple to prevent more than one person to vote.
Therefore, you should build your own two files in the program directory Data.dat and Votes.dat
Program Run Process part
The program should run with a voting HTML form, waiting for a vote.
When someone votes on an HTML form, determine if its IP is the IP of a recent voter,
If the person has just voted, the two values should be equal, showing "you have voted, thank you for your participation!" "
If the person does not vote, the two values are not equal, and its vote is written to the file (Votes.dat)
(The IP method of getting the voter here is that the Web client needs to provide a table when sending a request to the server,
This form of knowledge is stored in the environment variable REMOTE_ADDR, which we refer to.)
The final result is to show the poll results, usually by "bar chart + percent".
So, we want to select a single color rectangular shape to fit the requirements of horizontal scaling.
--------------------------------------------------------------------------------
Program code section to download program code
The voting column stores the file, you can change the file name
$data = "Data.dat";
Voting Results Save File
$votes = "Votes.dat";
Graphic filename of the graph file name used to display the poll result proportions,
The rectangle with a single color is better, and the poll results are easily scaled horizontally.
Upload yourself to this program directory
$path _img= "Vote.gif";
Vote title
$poll _name= "which section of mine do you like? ";
The following sections do not need to be changed//
Open the Voting column file, and in the form of an array of all the contents of the variable $dataf in the alternate
$dataf =file ($data);
if ($go!=1)
{
Show Voting program form
echo " $poll _name
n ";
echo "";
echo "View results";
}
Else
{
$go ==1 said that there was a vote,//The vote is now read in another way from the document $file _votes=fopen ($votes, "R");
$line _votes=fgets ($file _votes, 255);
Fclose ($file _votes);
And cuts the data into the specified string, and then passes the string back to the array variable.
$single _vote=explode ("|", $line _votes);
Prepare to write poll results
if ($result!=1)
{
Check if IP addresses are duplicated
$file _votes=file ($votes, "R");
if ($REMOTE _addr = = $file _votes[1])
{
echo " You have voted, thank you for your participation! ";
Exit
}
If IP is not duplicated, execute the following program
$ficdest =fopen ($votes, "w");
for ($i =0; $i <=count ($DATAF)-1; $i + +)
{
Judging by the cast of that column
if ($i = = $vote)
{
$single _vote[$i]+=1;
}
Write data back to file
Fputs ($ficdest, "$single _vote[$i]|");
}
Write to voter IP
Fputs ($ficdest, "n$remote_addr");
Fclose ($ficdest);
$result = 1; Vote successful
}
After the poll results are written and the poll results are displayed
if ($result ==1)
{echo "











"; for ($i =0; $i <=count ($DATAF)-1; $i + +) {//Total number of votes $tot _votes+= $single _vote[$i];} for ($i =0; $i <=count ($DATAF)-1; $i + +) {//Calculate percentage $stat [$i]= $single _vote[$i]/$tot _votes*100; echo " "; } echo "
  • ";
    echo "$dataf [$i]
  • ";
    echo "";
    Output percent printf ("%.1f", "$stat [$i]");
    echo "%
    "; Output this column voting number
    echo "$single _vote[$i]"; echo "

    ";
    echo "Total votes: $tot _votes";
    }
    }
    ?>

    http://www.bkjia.com/PHPjc/318055.html www.bkjia.com true http://www.bkjia.com/PHPjc/318055.html techarticle Analysis: We use a file (Data.dat) to store the voting columns. Each column occupies one row. This way, you can add and subtract the columns you want to vote for. We use a file ( ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.