Home  >  Article  >  Backend Development  >  A simple PHP voting program source code_PHP tutorial

A simple PHP voting program source code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:56:331079browse

Analysis:
We use a file (data.dat) to store the voting columns. Each column occupies one line.
In this way, you can add and subtract the columns you want to vote at will.
We will then Use a file (votes.dat) to store our voting results.
And record the IP address of the latest voter to simply prevent one person from voting more than once.
So, you should create it yourself in the program directory Two files data.dat and votes.dat
Part of the program running process
When the program is running, there should first be a voting HTML form, waiting for voting.
When someone votes on the HTML form, determine its Whether the IP is the IP of the most recent voter,
If this person has just voted, the two values ​​​​should be equal, showing "You have already voted, thank you for your participation!"
If this person has just voted If you have not voted, the two values ​​are not equal, write its vote to the file (votes.dat)
(The method to get the voter’s IP here is: the Web client needs to provide a table when sending a request to the server
This representation is stored in the environment variable REMOTE_ADDR, we can just reference it.)
Finally, the voting results are displayed. Usually we use "bar chart + percentage" to display the voting situation.
So, we need to choose a rectangular shape with a single color to adapt to the requirements of horizontal scaling.
-------------------------- -------------------------------------------------- ----
The program code part can be downloaded
//The voting column stores the file, you can change the file name
$data="data.dat";
//The voting result storage file
$votes="votes.dat";
//Graphic file name used to display the proportion of voting results,
//It is better to use a single-color rectangle to facilitate horizontal scaling of voting results,
//Upload it to the directory of this program by yourself
$path_img="vote.gif";
//Poll title
$poll_name="Which of my columns do you like?";
// The following parts do not need to be changed //
//Open the voting column file and save all the contents in the variable $dataf in the form of an array for later use.
$dataf=file($data);
if ( $go !=1)
{
//Display voting program form
echo "$poll_name
n";
echo "

";
for ($i=0; $i<=count($dataf)-1; $i++)
{
echo " $dataf[$i]
n"; }
echo "";
echo "

";
echo "

";
echo "View results";
}
else
{
// $go==1 means someone voted, // Now it will The votes are read from the file in another way $file_votes=fopen($votes, "r");
$line_votes=fgets($file_votes, 255);
fclose($file_votes);
// Cut the data according to the specified string, and then transfer the string back to the array variable
$single_vote=explode("|", $line_votes);
//Prepare to write the voting results
if ($result!=1)
{
//Check if the IP address is duplicated
$file_votes=file($votes, "r");
if ($REMOTE_ADDR == $file_votes[1])
{
echo "
You have already voted, thank you for your participation!  
";
exit;
}
//If the IP is not repeated, execute the following procedure
$ficdest=fopen($votes, "w") ;
for ($i=0; $i<=count($dataf)-1; $i++)
{
//Judge which column to vote for
if ($i == $ vote)
{
$single_vote[$i]+=1;
}
//Write the data back to the file
fputs($ficdest, "$single_vote[$i]|" );
}
//Write the voter IP
fputs($ficdest, "n$REMOTE_ADDR");
fclose($ficdest);
$result=1; // Voting successful
}
//Write the voting results and display the voting results
if ($result==1)
{ echo "";
for ($i=0; $i<=count($dataf)-1; $i++)
{
//Get the total number of votes
$tot_votes+=$single_vote[$i];
}
for ($i=0; $i<=count($dataf)-1; $i++)
{
//Calculate the percentage
$stat[$i]=$single_vote[$ i]/$tot_votes*100;
echo "< ;td align=center>"; //Output the number of votes in this column
echo "$single_vote[$i]
"; echo "";
}
echo "
  • "; /font>
  • ";
    echo " ";
    //Output percentage printf("%.1f", "$stat[$i]");
    echo "%

    ";
    echo "Total votes: $tot_votes < /font>";
    }
    }
    ?>

    http://www.bkjia.com/PHPjc/318055.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318055.htmlTechArticleAnalysis: We use a file (data.dat) to store the voting columns. Each column occupies one line. This way Come, you can add and subtract the columns you want to vote at will. We then use a file (...
    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn