PHP Example Analysis: voting procedures

Source: Internet
Author: User
Tags count html form php example


This is a simple voting procedure that does not require database support. This procedure is easy to understand, easy to use, is a good starting procedure. Disadvantages: Too much time to occupy the server, to be improved.

Free to use

Analysis:
We use a file (Data.dat) to store voting columns. Each column occupies one line.
In this way, you can add and subtract the columns you want to vote at will.

We then use a file (Votes.dat) to store our voting results.
And record the latest voter's IP address, simple to prevent a person to vote more.

Therefore, you should create two files for yourself under the directory Data.dat and Votes.dat

Part of program running process

The program should run with an HTML form for the ballot and wait for a vote.
When someone votes on an HTML form, determine whether its IP is the IP of the nearest voter,
If this person has just voted, then two values should be equal, indicating that "you have voted, thank you for your participation!" "
If the person has not voted, then the two values are unequal, and their votes are written to the file (Votes.dat)
(The IP method for voters here is that the Web client needs to provide a table when sending requests to the server.
This form is stored in the environment variable REMOTE_ADDR, and we can quote it.

Finally, the voting results are displayed, usually by "bar chart + percentage".
So, we want to select a single color rectangular shape to accommodate the requirement of scaling horizontally.

Program Code Section

Voting columns store files, you can change the file name
$data = "Data.dat";

The results of the ballot paper storage
$votes = "Votes.dat";

Graphic file name graphic file name used to display the proportional voting result,
The rectangle with a single color is better, so that the voting results are scaled horizontally,
Upload yourself to this program directory
$path _img= "Vote.gif";

Vote title
$poll _name= "which column do you like?" ";


The following sections do not need to be altered//


Open the Voting column file, and in the form of an array of all the contents of the variable $DATAF standby
$dataf =file ($data);
if ($go!=1)

{
Show voting procedure Form
echo "<font color=red><i> $poll _name</i></font><br>\n";
echo "<form method=post>";
For ($i =0 $i <=count ($DATAF)-1; $i + +)

{
echo "<input type=radio name=\" vote\ value=\ "$i \" > $dataf [$i]<br>\n]; }

echo "<input Type=hidden name=go value=1>";
echo "<p><input type=submit value= vote >";
echo "</form>";
echo "<a href= ' vote.php?result=1&go=1 ' > View results </a>";

}

Else

{
$go ==1 said that there was a vote,//Now the vote was read out in a different way from the document $file _votes=fopen ($votes, "R");
$line _votes=fgets ($file _votes, 255);
Fclose ($file _votes);

And the data is cut into the specified string, then the string is passed back into the array variable
$single _vote=explode ("|"), $line _votes);

Prepare to write the voting results
if ($result!=1)

{
Check to see if the IP address is duplicated
$file _votes=file ($votes, "R");
if ($REMOTE _addr = = $file _votes[1])

{
echo "<center><font color=red> you have voted, thank you for your participation!" </font> </center> ";
Exit
}

If IP is not duplicated, execute the following program
$ficdest =fopen ($votes, "w");
For ($i =0 $i <=count ($DATAF)-1; $i + +)

{
Judge voted for that column
if ($i = = $vote)

{
$single _vote[$i]+=1;
}

Write data back to a file
Fputs ($ficdest, "$single _vote[$i]|");

}

Write Voter IP
Fputs ($ficdest, "\n$remote_addr");
Fclose ($ficdest);
$result = 1; Voted successfully

}


After writing the voting results and showing the results
if ($result ==1)

{echo "<table cellpadding=10>";
For ($i =0 $i <=count ($DATAF)-1; $i + +)

{
Total number of votes obtained
$tot _votes+= $single _vote[$i];
}

For ($i =0 $i <=count ($DATAF)-1; $i + +)

{
Calculate percent
$stat [$i]= $single _vote[$i]/$tot _votes*100;
echo "<tr><td><li><font Face=verdana size=2>";
echo "$dataf [$i]</font></td><td align=left><font face=verdana size=2>";
echo "Output percent printf ("%.1f", "$stat [$i]");
echo "%</font></td><td Align=center><font face=verdana size=2>"; Output the number of votes in this column
echo "$single _vote[$i]</font>"; echo "</td></tr>";
}

echo "</table><p>";
echo "<font Face=verdana size=2> Total number of votes: $tot _votes </font>";
}

}

?>



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.