Home  >  Article  >  Backend Development  >  PHP online survey system_PHP tutorial

PHP online survey system_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:04:27815browse

PHP online survey system
This is a simple voting program, which is a very good introductory program for friends who are just learning PHP. Here I will introduce it to you, I hope it will be helpful to my friends.
The system is composed of the following four files: survey.htm with HTML survey form, survey.php that implements the survey function, data.txt that records survey items, and survey.txt that records survey results. Among them, data. txt and survey.txt, we can use NOTEPAD to create them respectively and transfer them to the program directory. The file data.txt stores the items to be investigated. Note that each item should occupy one line; survey.txt can be an empty file with no content. The code of
Survey.htm can be as follows:


survey



Survey item one


Survey item two


< p>Survey item three


Survey item four


Survey item five




View results


Note that the number and order of the survey items in the file data.txt must be consistent with the survey items above, otherwise errors will occur or the survey results will be inaccurate. At the same time, in order to display the survey results in the form of a bar chart, several bar pictures of different colors should be prepared.Such as: 0.gif, 1.gif, 2.gif, 3.gif, 4.gif, etc.
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 file*/
$file_votes=fopen($ votes, "r");
$line_votes=fgets($file_votes, 255); /*Read out the recorded survey results*/
fclose($file_votes);
$single_vote=explode(" |", $line_votes); /* and cut the data according to the specified string, and then transfer the string back to the array variable */
if ($result!=1) /*If the survey has been accepted */
{
$file_votes=file($votes, "r");
if ($REMOTE_ADDR == $file_votes[1]) /*Check if they are the same person*/
{
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++)
{/*Determine which item is selected*/
$ SINGLE_VOTE [$ i]+= 1;
}
FPUTS ($ FICDEST, "$ SINGLE_VOTE [$ i] |"); Write back the file*/
}
fputs($ficdest, "n$REMOTE_ADDR");/* //Write the voter IP*/
fclose($ficdest);
$result= 1; /*Vote successfully*/
}

/*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++)
{
$imag =strval($i).".gif";/*Determine which bar picture to use to display statistical results*/
$stat[$i]=$single_vote[$i]/$tot_votes*100; / *Calculate the percentage*/
$scla=$stat[$i]*5;/*Bar chart and magnification, here it is displayed by the width of the pixel that is 5 times the percentage*/
echo " ";
}
echo "
  • ";
      echo "$dataf[$i]
  • ";
    echo " ";/*Output barcode image*/
    printf("%.1f", "$stat[$i]");
    echo "%
    ";
    /*Output the number of votes in this column*/
    echo "$single_vote[$i]
    ";
    echo "

    ";
    echo "Total votes: $tot_votes ";
    }
    ?>
    Explanation:
    Here, in order to prevent one person from voting multiple times, the IP of the most recent voter is recorded, and the IP of the most recent voter is recorded The address is stored in the environment variable REMOTE_ADDR when the WEB client makes a request to the server.
    I am also a beginner. There may be many errors and inappropriatenesses in this article. I welcome your valuable comments and suggestions. Thanks!
    Personal Homepage: http://bamboo.oso.com.cn
    Email:wangyy@363.net


    http://www.bkjia.com/PHPjc/315912.html

    truehttp: //www.bkjia.com/PHPjc/315912.htmlTechArticlePHP online survey system This is a simple voting program, which is very good for friends who are just learning PHP. introductory program. Here I would like to introduce it to you, I hope it can be helpful to my friends...
    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