PHP Example: Online poll system written in PHP

Source: Internet
Author: User
Tags count php and php code php example variable

This is a simple voting procedure, for just learning PHP and friends is a very good starter program. Here to introduce you, hope to be able to help friends.

The system is composed of the following four files: The survey.htm of the HTML survey form, the survey.php to realize the investigation function, Record the data.txt of the investigation project and record the survey.txt of the investigation results. Data.txt and Survey.txt We can create them in Notepad and upload them to the program directory. The file data.txt contains items to be investigated, note that each project should occupy one line, and survey.txt can be an empty file with nothing.

The survey.htm code can be as follows:

<title>survey</title>
<body >
<form method= "POST" action= "survey.php" >
  <p><input type= "Radio" value= "0" name= " Vote "> Research Project One </p>
  <p><input type=" Radio "name=" Vote "value=" 1 "> Investigation Project two </p>
  <p><input type= "Radio" name= "Vote" value= "2" > Survey items three </p>
  <p><input type= " Radio "Name=" Vote "value=" 3 "> Research project four </p>
  <p><input type=" Radio "name=" Vote "value=" 4 "> Research items five </p>
  <p><input type= "hidden" name= "Go"   value= "1" >
  <p>< Input type= "Submit" value= "submitted" name= "B1" ></p>
  <a href= "survey.php?result=1" > View results </a>
</form>
</body>

Note that the survey items in the file data.txt must be consistent with the number and arrangement of the above survey items, or they may be wrong or the results of the investigation will not be accurate. In order to display the survey results in a bar chart, you should prepare a number of different color bar pictures. such as: 0.gif,1.gif,2.gif,3.gif,4.gif and so on.

The following is the survey.php code to implement the survey function:

?
$data = "Data.txt";
$votes = "Survey.txt";
$dataf =file ($data); /* Read the items in the survey project document * *
$file _votes=fopen ($votes, "R");
$line _votes=fgets ($file _votes, 255); * * read out the results of the investigation as recorded.
Fclose ($file _votes);
$single _vote=explode ("|"), $line _votes); /* and the data is cut into the specified string, then the string is passed back to the array variable.
if ($result!=1)/* If the investigation has been accepted * *
{
$file _votes=file ($votes, "R");
if ($REMOTE _addr = = $file _votes[1])/* Check is not the same person * *
{
echo "<center><font color=red> you have voted, thank you for your participation!" </font></center> ";
Exit
}
/* If IP does not repeat, execute the following procedure * *
$ficdest =fopen ($votes, "w");
For ($i =0 $i <=count ($DATAF)-1; $i + +)
{
if ($i = = $vote)
{/* Determine which item is selected. *
$single _vote[$i]+=1;
}
Fputs ($ficdest, "$single _vote[$i]|"); /* Write data back to File/*
}
Fputs ($ficdest, "\n$remote_addr");///written to voters ip*/
Fclose ($ficdest);
$result = 1; * * Voted successfully * *
}
* * Write the results of the poll and show the result of the poll
if ($result ==1)
{
echo "<table cellpadding=10>";
For ($i =0 $i <=count ($DATAF)-1; $i + +)
{
* * Get Total votes
$tot _votes+= $single _vote[$i];
}
For ($i =0 $i <=count ($DATAF)-1; $i + +)
{
$imag =strval ($i). " GIF ";/* determine which bar picture to use to display statistical results. *
$stat [$i]= $single _vote[$i]/$tot _votes*100; /* percent Calculated * *
$SCLA = $stat [$i]*5;/* bar chart and magnification, here is 5 times times the width of the phase element of the Ann percentage to display. *
echo "<tr><td><li><font Face=verdana size=2>";
echo "$dataf [$i]</font></td><td align=left><font face=verdana size=2>";
echo "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>";
}
?>

  Description:

In order to prevent a single person from being cast is implemented by recording the IP of the nearest voter, and the most recent one is that the Web client is stored in the environment variable REMOTE_ADDR when making a request to the server.

I am also a beginner, about this article may have many mistakes and the improper place welcome everybody to present the valuable opinion and the suggestion. Thank you!



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.